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:
Can someone explain what local (*err) = shift means?

 



perlFun
Novice

Jun 14, 2013, 2:26 PM

Post #1 of 5 (457 views)
Can someone explain what local (*err) = shift means? Can't Post

There is a line when I first enter a subroutine that reads:

local (*err) = shift;

I know that local refers to the scope of this variable (like a "my" or an "our", but for "local" it designates that it will only be within this subroutine), and shift takes the very first variable out of the @_ array (which contains the args passed in to the sub). I am unclear what the (*err) is. I am assuming err is the name being assigned to the variable but what about the use of parens around it as well as the *? I am unsure and wasn't able to find the answer and was wondering if anyone might be able to tell me.


Thank you so much!


(This post was edited by perlFun on Jun 14, 2013, 2:27 PM)


wickedxter
User

Jun 14, 2013, 3:01 PM

Post #2 of 5 (451 views)
Re: [perlFun] Can someone explain what local (*err) = shift means? [In reply to] Can't Post

maybe this will help
http://www.nntp.perl.org/group/perl.perl5.porters/2007/09/msg128170.html


perlFun
Novice

Jun 14, 2013, 3:15 PM

Post #3 of 5 (447 views)
Re: [wickedxter] Can someone explain what local (*err) = shift means? [In reply to] Can't Post

thanks for this! actually now I'm a little bit more confused. I had assumed err was a var name the programmer who wrote this code I'm reading had named the variable. But it's a perl keyword? but from these discussions itl ooks like its been removed as a keyword? so how would I tell if it's actually a keyword in the context of the code I'm reading or if it's just a variable name?

also, what does the * do, as well as putting it in parens? (*err) ?

thanks so much!


BillKSmith
Veteran

Jun 14, 2013, 7:53 PM

Post #4 of 5 (440 views)
Re: [perlFun] Can someone explain what local (*err) = shift means? [In reply to] Can't Post

I believe that err is the name of a global variable used to store a filehandle.


Code
use strict; 
the_subroutine(*STDOUT);
exit(0);

sub the_subroutine {
local (*err) = shift;
print err "Can you read this?\n";
}


The parenthesis around the argument of local are optional.

Note: The book "Perl Cookbook" covers this in some detail in the recipe "Storing Filehandles in Variables".
Good Luck,
Bill


FishMonger
Veteran / Moderator

Jun 15, 2013, 7:54 AM

Post #5 of 5 (422 views)
Re: [BillKSmith] Can someone explain what local (*err) = shift means? [In reply to] Can't Post

The global var doesn't need to store a filehandle and it doesn't need to be a scalar.


Code
#!/usr/bin/perl 

use 5.010;
use strict;
use warnings;
use Data::Dumper;

our @err = qw(array of errors);
mysub(@err);

say "after sub\n", Dumper \@err;

sub mysub {
local (*err) = shift;
@err = 'some error';
say "inside sub\n", Dumper \@err;
}


Outputs:

Code
inside sub 
$VAR1 = [
'some error'
];
after sub
$VAR1 = [
'array',
'of',
'errors'
];

D:\test>perl-1.pl
inside sub
$VAR1 = [
'some error'
];

after sub
$VAR1 = [
'array',
'of',
'errors'
];


 
 


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

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