LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2006, 11:38 AM   #1
sporty
LQ Newbie
 
Registered: Oct 2005
Location: Austin, Tx
Posts: 18

Rep: Reputation: 0
perl - passing values to subroutines


I am having some trouble understanding how to reference a value passed to a subroutine.

When I run the following code the first print statement in sub open_domain prints the value passed in from the sub open_states; however the second print statement in open_domain prints the same value as the $_ which was read from the file domain.list.
I'm confused, I thought the second print statement would print the value read in from the file domain.list and the value passed in from the sub open_states.

Could someone please explain to me why the @_ in the second print statement does not print the passed in value and how I correct this?
Thanks

Code:
#!/usr/bin/perl -w
sub open_domain {
     print "@_";
   open (DOM, "<", "domain.list");
        while (<DOM>) {
          print "$_",".","@_";
        }
   close DOM;
}   # end open_dom

sub open_states {
        open (STATES, "<", "abv.list");

        while (<STATES>) {
                open_dom ($_);
        } #end while STATES
        close STATES;
} # end sub open_states

open_states ();
 
Old 08-07-2006, 12:44 PM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I guess you want something like this:
Code:
sub open_domain {
   my $arg = shift;
   print $arg;
   open (DOM, "<", "domain.list");
        while (<DOM>) {
          print "$_",".",$arg;
        }
   close DOM;
}   # end open_dom
 
Old 08-07-2006, 01:37 PM   #3
sporty
LQ Newbie
 
Registered: Oct 2005
Location: Austin, Tx
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by spirit receiver
I guess you want something like this:
Code:
sub open_domain {
   my $arg = shift;
   print $arg;
   open (DOM, "<", "domain.list");
        while (<DOM>) {
          print "$_",".",$arg;
        }
   close DOM;
}   # end open_dom
Thanks spirit receiver. I guess from this code that the @_ is only good if you dont read anymore data from another file because then the array @_ is used inside the sub for the read- correct?


Thanks again
 
Old 08-07-2006, 02:53 PM   #4
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Seems like you're asking a good question here, I couldn't reproduce your problem.

What does the following script return in your case?
Code:
#! /usr/bin/perl

use warnings;
use strict;

sub nuff {
  open( FILE, "< tempdata-$$");
  while (<FILE>) {
    print "$_","@_","\n";
   }
  close( FILE );
}

-e "tempdata-$$" && die;
open( DATA , "> tempdata-$$" );
print DATA "I am read from the file.\nSo am I.\nMe too.\n";
close( DATA );

my @array = ("arg 1,","arg 2,","arg 3");
nuff @array;
system "rm tempdata-$$";
This is what I get:
Code:
ada@barnabas:~/tmp> ./test.pl
I am read from the file.
arg 1,arg 2,arg 3
So am I.
arg 1,arg 2,arg 3
Me too.
arg 1,arg 2,arg 3
 
Old 08-07-2006, 04:30 PM   #5
sporty
LQ Newbie
 
Registered: Oct 2005
Location: Austin, Tx
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by spirit receiver
This is what I get:
Code:
ada@barnabas:~/tmp> ./test.pl
I am read from the file.
arg 1,arg 2,arg 3
So am I.
arg 1,arg 2,arg 3
Me too.
arg 1,arg 2,arg 3
I got the same output as you running your code.

Code:
I am read from the file.
arg 1, arg 2, arg 3
So am I.
arg 1, arg 2, arg 3
Me too.
arg 1, arg 2, arg 3
The more I learn about perl the less I know.
Thanks again
 
Old 08-07-2006, 05:27 PM   #6
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I tried your script, and I also got that strange behaviour. Note that @_ will even be uninitialized if accessed from open_domain after the inner loop.
The content of @_ seems to be somewhat undetermined in this situation, doesn't it?
 
Old 08-08-2006, 08:29 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
got any example data files???
 
Old 08-08-2006, 09:13 AM   #8
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
The data is completely irrelevant, just create two files, domain.list and abv.list, each with a single line of text. There's a typo in sporty's script, the subroutine should be named open_dom instead of open_domain.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
(Perl) Dereferencing arrays for subroutines Poetics Programming 7 05-23-2006 05:27 PM
Passing form values between multiple forms!! AskMe Programming 5 09-07-2005 07:44 PM
Sed Command & Passing Values joey52 Linux - Newbie 4 12-27-2004 07:46 PM
seperating values using perl pantera Programming 3 05-26-2004 12:31 AM
passing values in shell pantera Programming 1 05-20-2004 09:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration