ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ();
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?
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
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.