LinuxQuestions.org
Register a domain and help support LQ
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
 
Thread Tools
Old 08-07-2006, 12:38 PM   #1
sporty
LQ Newbie
 
Registered: Oct 2005
Location: Austin, Tx
Posts: 18
Thanked: 0
perl - passing values to subroutines


[Log in to get rid of this advertisement]
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 ();
sporty is offline     Reply With Quote
Old 08-07-2006, 01:44 PM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 425
Thanked: 0
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
spirit receiver is offline     Reply With Quote
Old 08-07-2006, 02:37 PM   #3
sporty
LQ Newbie
 
Registered: Oct 2005
Location: Austin, Tx
Posts: 18
Thanked: 0

Original Poster
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
sporty is offline     Reply With Quote
Old 08-07-2006, 03:53 PM   #4
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 425
Thanked: 0
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
spirit receiver is offline     Reply With Quote
Old 08-07-2006, 05:30 PM   #5
sporty
LQ Newbie
 
Registered: Oct 2005
Location: Austin, Tx
Posts: 18
Thanked: 0

Original Poster
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
sporty is offline     Reply With Quote
Old 08-07-2006, 06:27 PM   #6
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 425
Thanked: 0
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?
spirit receiver is offline     Reply With Quote
Old 08-08-2006, 09:29 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,709
Thanked: 19
got any example data files???
bigearsbilly is offline     Reply With Quote
Old 08-08-2006, 10:13 AM   #8
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 425
Thanked: 0
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.
spirit receiver is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 06:27 PM
Passing form values between multiple forms!! AskMe Programming 5 09-07-2005 08:44 PM
Sed Command & Passing Values joey52 Linux - Newbie 4 12-27-2004 08:46 PM
seperating values using perl pantera Programming 3 05-26-2004 01:31 AM
passing values in shell pantera Programming 1 05-20-2004 10:01 AM


All times are GMT -5. The time now is 03:09 AM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration