[29493] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 737 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 9 16:19:37 2007

Date: Thu, 9 Aug 2007 13:19:13 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 9 Aug 2007     Volume: 11 Number: 737

Today's topics:
    Re: pass by reference <mritty@gmail.com>
    Re: pass by reference <jgibson@mail.arc.nasa.gov>
    Re: pass by reference <uri@stemsystems.com>
    Re: pass by reference <larry.grant.dc@gmail.com>
    Re: pass by reference <larry.grant.dc@gmail.com>
    Re: pass by reference <larry.grant.dc@gmail.com>
    Re: pass by reference <bpatton@ti.com>
    Re: pass by reference <larry.grant.dc@gmail.com>
    Re: pass by reference <uri@stemsystems.com>
    Re: pass by reference <uri@stemsystems.com>
    Re: pass by reference <uri@stemsystems.com>
    Re: pass by reference <larry.grant.dc@gmail.com>
    Re: pass by reference <larry.grant.dc@gmail.com>
    Re: Replacing a line <glex_no-spam@qwest-spam-no.invalid>
    Re: Size of all files in a directory? <dummy@example.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 09 Aug 2007 11:16:14 -0700
From:  Paul Lalli <mritty@gmail.com>
Subject: Re: pass by reference
Message-Id: <1186683374.978210.80010@q75g2000hsh.googlegroups.com>

On Aug 9, 2:05 pm, Larry <larry.grant...@gmail.com> wrote:

> > Larry <larry.grant...@gmail.com> writes:
> > > The following code snippet shows how I do "pass by
> > > reference" in Perl.  It works fine, but I'm wondering...
> > > is there a less verbose way to do this?

> I don't like the $_[0] method, because it doesn't give me a
> chance to name my variables.... I prefer names, not numbers,
> thank you!  

So use your name the entire time you're actually doing something with
the variable, then change it back at the end:

$ perl -le'
my $x = 7;
squareMe($x);
print $x;

sub squareMe {
   my ($arg) = @_;
   $arg **= 2;
   $_[0] = $arg;
}
'
49


Paul Lalli



------------------------------

Date: Thu, 09 Aug 2007 11:28:07 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: pass by reference
Message-Id: <090820071128077402%jgibson@mail.arc.nasa.gov>

In article <1186682747.757896.223810@o61g2000hsh.googlegroups.com>,
Larry <larry.grant.dc@gmail.com> wrote:

> On Aug 9, 2:04 pm, Peter Makholm <pe...@makholm.net> wrote:
> > Larry <larry.grant...@gmail.com> writes:
> > > The following code snippet shows how I do "pass by reference" in
> > > Perl.  It works fine, but I'm wondering... is there a less verbose way
> > > to do this?
> >
> > Perl already implements a kind of pass by reference in the @_
> > argument.
> >
> > #!/usr/bin/perl -l
> > sub squareMe {
> >     $_[0] *= $_[0];


> 
> I don't like the $_[0] method, because it doesn't give me a chance to
> name my variables.... I prefer names, not numbers, thank you!  :)
> 

$_ is a name, just not a pronounceable one!

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


------------------------------

Date: Thu, 09 Aug 2007 18:59:41 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: pass by reference
Message-Id: <x7hcn8leji.fsf@mail.sysarch.com>

>>>>> "L" == Larry  <larry.grant.dc@gmail.com> writes:

  L> I don't like the $_[0] method, because it doesn't give me a chance to
  L> name my variables.... I prefer names, not numbers, thank you!  :)

so pass in a reference. the typeglob hack you created is fugly and
wasteful. and don't use prototypes as they are not what you want here. a
plain simple reference works fine.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: Thu, 09 Aug 2007 19:07:12 -0000
From:  Larry <larry.grant.dc@gmail.com>
Subject: Re: pass by reference
Message-Id: <1186686432.552257.78870@e16g2000pri.googlegroups.com>

On Aug 9, 2:59 pm, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "L" == Larry  <larry.grant...@gmail.com> writes:
>
>   L> I don't like the $_[0] method, because it doesn't give me a chance to
>   L> name my variables.... I prefer names, not numbers, thank you!  :)
>
> so pass in a reference. the typeglob hack you created is fugly and
> wasteful. and don't use prototypes as they are not what you want here. a
> plain simple reference works fine.
>

I don't want to pass in a reference!  You call that "passing by
reference"?!  :)

I want to write:

  squareMe $foo

not

  squareMe \$foo

!!!



------------------------------

Date: Thu, 09 Aug 2007 19:08:19 -0000
From:  Larry <larry.grant.dc@gmail.com>
Subject: Re: pass by reference
Message-Id: <1186686499.683528.189600@g12g2000prg.googlegroups.com>

On Aug 9, 3:07 pm, Larry <larry.grant...@gmail.com> wrote:
> On Aug 9, 2:59 pm, Uri Guttman <u...@stemsystems.com> wrote:
>
> > >>>>> "L" == Larry  <larry.grant...@gmail.com> writes:
>
> >   L> I don't like the $_[0] method, because it doesn't give me a chance to
> >   L> name my variables.... I prefer names, not numbers, thank you!  :)
>
> > so pass in a reference. the typeglob hack you created is fugly and
> > wasteful. and don't use prototypes as they are not what you want here. a
> > plain simple reference works fine.
>
> I don't want to pass in a reference!  You call that "passing by
> reference"?!  :)
>
> I want to write:
>
>   squareMe $foo
>
> not
>
>   squareMe \$foo
>
> !!!

And I *do* need the prototypes... that's the only way to get the
reference without my callers having to use backslashes!



------------------------------

Date: Thu, 09 Aug 2007 19:10:37 -0000
From:  Larry <larry.grant.dc@gmail.com>
Subject: Re: pass by reference
Message-Id: <1186686637.717608.54860@e9g2000prf.googlegroups.com>

On Aug 9, 1:50 pm, Larry <larry.grant...@gmail.com> wrote:
> The following code snippet shows how I do "pass by reference" in
> Perl.  It works fine, but I'm wondering... is there a less verbose way
> to do this?
>
> (I know in this simple example, I could avoid the typeglob assignment
> and just use $$arg instead of $arg, but in a more complex usage, that
> would bug me... I'd rather just use the more normal-looking $arg
> everywhere.)
>
> -------------
>
> sub squareMe (\$) {
>     my ($argRef) = @_;
>     local *arg = $argRef;
>     our $arg;
>
>     $arg *= $arg;
>
> }
>
> my $n = 7;
> squareMe $n;
> print "$n\n";  # prints 49

I just figured out a somewhat more elegant way:

 sub squareMe (\$) {
     local (*arg) = @_;
     our $arg;

     $arg *= $arg;

 }

Too bad about the need for having to repeat "arg" in "our" though...



------------------------------

Date: Thu, 09 Aug 2007 12:59:16 -0500
From: Billy Patton <bpatton@ti.com>
Subject: Re: pass by reference
Message-Id: <f9fklk$ksa$1@home.itg.ti.com>

Larry wrote:
> The following code snippet shows how I do "pass by reference" in
> Perl.  It works fine, but I'm wondering... is there a less verbose way
> to do this?
> 
> (I know in this simple example, I could avoid the typeglob assignment
> and just use $$arg instead of $arg, but in a more complex usage, that
> would bug me... I'd rather just use the more normal-looking $arg
> everywhere.)
> 
> -------------
> 
> sub squareMe (\$) {
>     my ($argRef) = @_;
>     local *arg = $argRef;
>     our $arg;
> 
>     $arg *= $arg;
> }
> 
> my $n = 7;
> squareMe $n;
> print "$n\n";  # prints 49
> 

Do you really want side effects ?
Passing by reference does this.
Guess this is too simple to make that decision.

sub squareMe($) {
   my ($arg) = @_;
   $arg *= $arg;
}

my $n=7;
$n = squareMe $n;  # no side effects
I hate it when my variables get changed somewhere else and I have to 
track it down for debugging!


------------------------------

Date: Thu, 09 Aug 2007 19:17:40 -0000
From:  Larry <larry.grant.dc@gmail.com>
Subject: Re: pass by reference
Message-Id: <1186687060.811271.110820@m37g2000prh.googlegroups.com>

On Aug 9, 1:59 pm, Billy Patton <bpat...@ti.com> wrote:
> Larry wrote:
> > The following code snippet shows how I do "pass by reference" in
> > Perl.  It works fine, but I'm wondering... is there a less verbose way
> > to do this?
>
> > (I know in this simple example, I could avoid the typeglob assignment
> > and just use $$arg instead of $arg, but in a more complex usage, that
> > would bug me... I'd rather just use the more normal-looking $arg
> > everywhere.)
>
> > -------------
>
> > sub squareMe (\$) {
> >     my ($argRef) = @_;
> >     local *arg = $argRef;
> >     our $arg;
>
> >     $arg *= $arg;
> > }
>
> > my $n = 7;
> > squareMe $n;
> > print "$n\n";  # prints 49
>
> Do you really want side effects ?
> Passing by reference does this.
> Guess this is too simple to make that decision.
>
> sub squareMe($) {
>    my ($arg) = @_;
>    $arg *= $arg;
>
> }
>
> my $n=7;
> $n = squareMe $n;  # no side effects
> I hate it when my variables get changed somewhere else and I have to
> track it down for debugging!

Sometimes, I do want side effects!  What if I know that every time I
call "squareMe" I will want to modify the var. being squared?

I'd rather write:

  squareMe $n

than

  $n = squareMe $n



------------------------------

Date: Thu, 09 Aug 2007 19:27:08 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: pass by reference
Message-Id: <x7k5s4jypc.fsf@mail.sysarch.com>

>>>>> "L" == Larry  <larry.grant.dc@gmail.com> writes:

  L> On Aug 9, 2:59 pm, Uri Guttman <u...@stemsystems.com> wrote:
  >> >>>>> "L" == Larry  <larry.grant...@gmail.com> writes:
  >> 
  L> I don't like the $_[0] method, because it doesn't give me a chance to
  L> name my variables.... I prefer names, not numbers, thank you!  :)
  >> 
  >> so pass in a reference. the typeglob hack you created is fugly and
  >> wasteful. and don't use prototypes as they are not what you want here. a
  >> plain simple reference works fine.
  >> 

  L> I don't want to pass in a reference!  You call that "passing by
  L> reference"?!  :)

sure is but it is just explicit. i know what pass by ref is.

  L> I want to write:

  L>   squareMe $foo

  L> not

  L>   squareMe \$foo

well, then use $_[0] as others have said. your typeglob answer is
horrible for such a simple issue. in my view you are working too hard
for a tiny syntactical gain. this is from a few weeks of coding
experience that i have :)

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: Thu, 09 Aug 2007 19:28:06 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: pass by reference
Message-Id: <x7fy2sjynp.fsf@mail.sysarch.com>

>>>>> "L" == Larry  <larry.grant.dc@gmail.com> writes:

  L> On Aug 9, 3:07 pm, Larry <larry.grant...@gmail.com> wrote:
  >> On Aug 9, 2:59 pm, Uri Guttman <u...@stemsystems.com> wrote:
  >> 
  >> > >>>>> "L" == Larry  <larry.grant...@gmail.com> writes:
  >> 
  >> >   L> I don't like the $_[0] method, because it doesn't give me a chance to
  >> >   L> name my variables.... I prefer names, not numbers, thank you!  :)
  >> 
  >> > so pass in a reference. the typeglob hack you created is fugly and
  >> > wasteful. and don't use prototypes as they are not what you want here. a
  >> > plain simple reference works fine.
  >> 
  >> I don't want to pass in a reference!  You call that "passing by
  >> reference"?!  :)
  >> 
  >> I want to write:
  >> 
  >> squareMe $foo
  >> 
  >> not
  >> 
  >> squareMe \$foo
  >> 
  >> !!!

  L> And I *do* need the prototypes... that's the only way to get the
  L> reference without my callers having to use backslashes!

nope again. they can get a ref earlier into another scalar and pass
that. your api design to make their life 'easier' is actually making it
harder. but do what you want. 

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: Thu, 09 Aug 2007 19:29:07 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: pass by reference
Message-Id: <x7bqdgjym1.fsf@mail.sysarch.com>

>>>>> "L" == Larry  <larry.grant.dc@gmail.com> writes:

  L> On Aug 9, 1:50 pm, Larry <larry.grant...@gmail.com> wrote:
  >> The following code snippet shows how I do "pass by reference" in
  >> Perl.  It works fine, but I'm wondering... is there a less verbose way
  >> to do this?
  >> 
  >> (I know in this simple example, I could avoid the typeglob assignment
  >> and just use $$arg instead of $arg, but in a more complex usage, that
  >> would bug me... I'd rather just use the more normal-looking $arg
  >> everywhere.)
  >> 
  >> -------------
  >> 
  >> sub squareMe (\$) {
  >> my ($argRef) = @_;
  >> local *arg = $argRef;
  >> our $arg;
  >> 
  >> $arg *= $arg;
  >> 
  >> }
  >> 
  >> my $n = 7;
  >> squareMe $n;
  >> print "$n\n";  # prints 49

  L> I just figured out a somewhat more elegant way:

  L>  sub squareMe (\$) {
  L>      local (*arg) = @_;
  L>      our $arg;

  L>      $arg *= $arg;

  L>  }

ewwwwww!

  L> Too bad about the need for having to repeat "arg" in "our" though...

double ewwwwww.

tell them to pass a real ref and be done with it. they need to learn
about refs anyhow and coddling your callers is doing them a big
disservice.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: Thu, 09 Aug 2007 19:39:36 -0000
From:  Larry <larry.grant.dc@gmail.com>
Subject: Re: pass by reference
Message-Id: <1186688376.999386.300820@g12g2000prg.googlegroups.com>

> tell them to pass a real ref and be done with it. they need to learn
> about refs anyhow and coddling your callers is doing them a big
> disservice.
>
> uri

I see your point, but I miss "real" passing by reference from Pascal
and Ada.  But C, Perl and Java seem to want to bury it :(



------------------------------

Date: Thu, 09 Aug 2007 19:40:49 -0000
From:  Larry <larry.grant.dc@gmail.com>
Subject: Re: pass by reference
Message-Id: <1186688449.373686.21240@i38g2000prf.googlegroups.com>

On Aug 9, 3:39 pm, Larry <larry.grant...@gmail.com> wrote:
> > tell them to pass a real ref and be done with it. they need to learn
> > about refs anyhow and coddling your callers is doing them a big
> > disservice.
>
> > uri
>
> I see your point, but I miss "real" passing by reference from Pascal
> and Ada.  But C, Perl and Java seem to want to bury it :(

And put C++ in with Pascal and Ada!



------------------------------

Date: Thu, 09 Aug 2007 14:06:40 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Replacing a line
Message-Id: <46bb65c0$0$3572$815e3792@news.qwest.net>

Ved wrote:
> On Aug 7, 3:17 am, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
> wrote:
>> Ved wrote:
>>> Hi all,
>>> I have to replace commented line in a list of  " .cpp" file .
>>> Line  looks like this:
>>> //#define BBPRxChanRouteFileLoadInput 1
>>> i.e. The line format :
>>> //#define (FileName)FileLoadInput 1
>>>  is to be replaced with
>>> #define (FileName)FileLoadInput 1
[...]
>>         for( @array )
>>         {
>>                 # Need to escape the $
>>                 if( m{^//#define \$dirFileLoadInput 1$} )
>>                 {
>>                         s{^//}{};
>>                         last;
>>                 }
>>         }
>>
>> Documentation on regular expressions:
>>
>>         perldoc perlretut
> Hi Gleixner,
> 
> Why is it not entering in this condition ?
> 
>  if( m{^//#define \$module_nameFileLoadInput 1$} )
> 

Sorry, my fault. I misssed that $dir was a variable, in
your match. For some reason I thought you were trying to
actually match a string that contained '$dirFileLoadInput'.

	${module_name}FileLoadInput


------------------------------

Date: Thu, 09 Aug 2007 18:46:32 GMT
From: "John W. Krahn" <dummy@example.com>
Subject: Re: Size of all files in a directory?
Message-Id: <ciJui.83080$tB5.1320@edtnps90>

Paul Lalli wrote:
> On Aug 9, 8:14 am, Paul Lalli <mri...@gmail.com> wrote:
>> On Aug 9, 1:08 am, "comp.llang.perl.moderated" <c...@blv-
>> sam-01.ca.boeing.com> wrote:
>>> On Aug 7, 3:20 pm, Paul Lalli <mri...@gmail.com> wrote:
>>>> there's nothing intuitively telling me readdir returned
>>>> false because there was an OS error, or because the
>>>> directory actually was empty.  I guess I would rather it
>>>>  throw an exception.
>>> You could get something very similar with Fatal's "succeed
>>> or die" semantics:
>> Interesting, I hadn't considered that.  Thanks for the idea.
> 
> Uhm, I conditionally take that back. ;-)   Fatal seems to severely
> FUBAR readdir().  What am I doing wrong?

$ perl -wle'
use Fatal qw/readdir/;
opendir DH, "." or die $!;
my @files = readdir DH;
print for @files;
'
Name "main::DH" used only once: possible typo at -e line 3.
test

It appears that the directory handle is invalidated at some point?


John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 737
**************************************


home help back first fref pref prev next nref lref last post