CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
how to pass value to subroutine

 



kvok
Novice

Sep 10, 2015, 7:43 AM

Post #1 of 3 (855 views)
how to pass value to subroutine Can't Post

Hi,

I try within following script pass value (string) to subroutine w2log but I get just number 1 not the whole string (date + variable var):


Code
#!/usr/bin/perl 
use warnings ;
use strict ;
sub w2log {
my $logent =@_;
print "$logent\n";
open (FILE, ">>log.log" ) || die "Cannot open file ";
print FILE $logent ;
close FILE;
}
print "Enter something: ";
my $var= <STDIN>;
my $date = qx(date +%D#%T);
chomp $date ;
my $logent= $date.$var;
print "$logent\n";
w2log();


any idea what might be wrong ?
Thanks a lot .


FishMonger
Veteran / Moderator

Sep 10, 2015, 7:53 AM

Post #2 of 3 (854 views)
Re: [kvok] how to pass value to subroutine [In reply to] Can't Post


Code
my $logent =@_;

That assignment is in scalar context. When an array is used in scalar context like that, it returns the number of elements. You need to change that assignment to list context.


Code
my ($logent) =@_;

or

Code
my $logent = shift;


BTW, you're not passing anything to the sub. I assume you meant to call the sub like this:

Code
w2log($logent);



(This post was edited by FishMonger on Sep 10, 2015, 7:56 AM)


kvok
Novice

Sep 11, 2015, 12:42 AM

Post #3 of 3 (839 views)
Re: [FishMonger] how to pass value to subroutine [In reply to] Can't Post

Thanks FishMonger,

its working now :)

yes I forgot to insert value to subroutine (typo)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives