Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A little fuzzy on subs

75 views
Skip to first unread message

T

unread,
Aug 2, 2015, 6:48:04 AM8/2/15
to
Hi All,

Guess who is reading through "perldoc perlsub"?


I am a little bit fuzzy as to what happens to values
that are passed to the sub. On line 92, they give an
example:

sub max {
my $max = shift(@_);
foreach $foo (@_) {
$max = $foo if $max < $foo;
}
return $max;
}
$bestday = max($mon,$tue,$wed,$thu,$fri);


I makes total sense. In Modula2 and Bash, I call
this a "function". It returns "a" value.

Now here is the fuzz. Can you use this thing as
a procedure (a.k.a a subroutine) where you can
modify the arguments you send it?

In other words, if I called the sub with variables
as the arguments and if I modify the value(s) of
@_[x] will the variables in the calling argument
also get modified? Or does whatever happens in the
sub not affect the calling arguments?

Many thanks,
-T

Rainer Weikusat

unread,
Aug 2, 2015, 7:46:13 AM8/2/15
to
T <T...@invalid.invalid> writes:
> Guess who is reading through "perldoc perlsub"?
>
>
> I am a little bit fuzzy as to what happens to values
> that are passed to the sub. On line 92, they give an
> example:
>
> sub max {
> my $max = shift(@_);
> foreach $foo (@_) {
> $max = $foo if $max < $foo;
> }
> return $max;
> }
> $bestday = max($mon,$tue,$wed,$thu,$fri);
>
>
> I makes total sense. In Modula2 and Bash, I call
> this a "function". It returns "a" value.
>
> Now here is the fuzz. Can you use this thing as
> a procedure (a.k.a a subroutine) where you can
> modify the arguments you send it?

The procedure/ function split is (AFAIK) another distinctive concept of
languages designed by Nikolaus Wirth. It's best if forget about it. C
(somewhat incorrectly) uses the term 'function' exclusively and in Perl,
it's (somewhat correctly) 'subroutine'.

> In other words, if I called the sub with variables
> as the arguments and if I modify the value(s) of
> @_[x] will the variables in the calling argument
> also get modified?

Every scalar value in Perl is ultimatively represented by a scalar (a
built-in Perl object) and invoking a subroutine with arguments causes
the actual scalars holding the arguments to be pushed onto the call
stack, IOW, yes if you're accessing the actual arguments directly, eg,
via $_[0], no if you copy the values into local variables, as in

my $max = shift;

This creates a scalar named $max and initializes it with a copy of the
value of the first argument (which is then dropped on the floor, ie,
removed from @_).

$Bill

unread,
Aug 2, 2015, 11:04:20 AM8/2/15
to
On 8/2/2015 03:48, T wrote:
> Hi All,
>
> Guess who is reading through "perldoc perlsub"?
>
>
> I am a little bit fuzzy as to what happens to values
> that are passed to the sub. On line 92, they give an
> example:
>
> sub max {
> my $max = shift(@_);
> foreach $foo (@_) {
> $max = $foo if $max < $foo;
> }
> return $max;
> }
> $bestday = max($mon,$tue,$wed,$thu,$fri);
>
>
> I makes total sense. In Modula2 and Bash, I call
> this a "function". It returns "a" value.
>
> Now here is the fuzz. Can you use this thing as
> a procedure (a.k.a a subroutine) where you can
> modify the arguments you send it?

Yes, if you pass in a reference to the scalar instead of the scalar itself.

> In other words, if I called the sub with variables
> as the arguments and if I modify the value(s) of
> @_[x] will the variables in the calling argument
> also get modified? Or does whatever happens in the
> sub not affect the calling arguments?

my $x = 9;
mysub (\$x);
sub mysub { ++${$_[0]}; }

print $x and get 10.

Jürgen Exner

unread,
Aug 2, 2015, 11:15:37 AM8/2/15
to
T <T...@invalid.invalid> wrote:
>I am a little bit fuzzy as to what happens to values
>that are passed to the sub. On line 92, they give an
>example:
>
> sub max {
> my $max = shift(@_);
> foreach $foo (@_) {
> $max = $foo if $max < $foo;
> }
> return $max;
> }
> $bestday = max($mon,$tue,$wed,$thu,$fri);
>
>
>I makes total sense. In Modula2 and Bash, I call
>this a "function". It returns "a" value.
>
>Now here is the fuzz. Can you use this thing as
>a procedure (a.k.a a subroutine)

There is no such differentiation Perl. The thingy is called a subroutine
and it may or may not return a list of values.

>where you can
>modify the arguments you send it?

Read perldoc perlsub carefully:
<quote>
[...] The array @_ is a local array, but its elements are aliases
for
the actual scalar parameters. In particular, if an element $_[0] is
updated, the corresponding argument is updated (or an error occurs
if it
is not updatable).
</quote>

In other words: argument handling is call-by-reference.

>In other words, if I called the sub with variables
>as the arguments and if I modify the value(s) of
>@_[x] will the variables in the calling argument
>also get modified?

What happened when you tried it?

jue

Rainer Weikusat

unread,
Aug 2, 2015, 1:02:58 PM8/2/15
to
$Bill <ne...@todbe.com> writes:
> On 8/2/2015 03:48, T wrote:

[...]

>> In other words, if I called the sub with variables
>> as the arguments and if I modify the value(s) of
>> @_[x] will the variables in the calling argument
>> also get modified? Or does whatever happens in the
>> sub not affect the calling arguments?
>
> my $x = 9;
> mysub (\$x);
> sub mysub { ++${$_[0]}; }
>
> print $x and get 10.

This is also true for

------
sub inc { ++$_[0]; }

my $x = 9;
inc($x);
print("$x\n");
------

T

unread,
Aug 2, 2015, 6:29:24 PM8/2/15
to
Thank you for the clarification!

The first thing I do in a function is to @_[x] to a "my"
variable. This for readability.

T

unread,
Aug 2, 2015, 6:38:29 PM8/2/15
to
On 08/02/2015 08:04 AM, $Bill wrote:
> On 8/2/2015 03:48, T wrote:
>> Hi All,
>>
>> Guess who is reading through "perldoc perlsub"?
>>
>>
>> I am a little bit fuzzy as to what happens to values
>> that are passed to the sub. On line 92, they give an
>> example:
>>
>> sub max {
>> my $max = shift(@_);
>> foreach $foo (@_) {
>> $max = $foo if $max < $foo;
>> }
>> return $max;
>> }
>> $bestday = max($mon,$tue,$wed,$thu,$fri);
>>
>>
>> I makes total sense. In Modula2 and Bash, I call
>> this a "function". It returns "a" value.
>>
>> Now here is the fuzz. Can you use this thing as
>> a procedure (a.k.a a subroutine) where you can
>> modify the arguments you send it?
>
> Yes, if you pass in a reference to the scalar instead of the scalar itself.

Makes sense.

>
>> In other words, if I called the sub with variables
>> as the arguments and if I modify the value(s) of
>> @_[x] will the variables in the calling argument
>> also get modified? Or does whatever happens in the
>> sub not affect the calling arguments?
>
> my $x = 9;
> mysub (\$x);

In "perldoc perlsub" lines 976 to 989, I am not finding
"\$x". Is this where you are passing the pointer (by
that I mean address) of "$x" to "mysub"?

Would you define for me what you are doing with "\$x"?
And was "\" an "escape" or a command?

> sub mysub { ++${$_[0]}; }

Is "++" the same thing as assembly code's INCREMENT (INC),
meaning to increment an Integer by 1?

And does "{$_[0]}" translate the pointer into the variable?

>
> print $x and get 10.
>

I only ask for clarifications o the above as I am going
to copy it into my keepers.

Thank you for helping me with this!

-T

T

unread,
Aug 2, 2015, 6:42:13 PM8/2/15
to
Hi Ranier,

I though "++$_[0]" was local to the sub. And
that it did not reflect back to the calling routine?

-T

T

unread,
Aug 2, 2015, 6:53:37 PM8/2/15
to
Lines 56 to 66. And I missed it! Thank you!

Same way in Modula2.

So, if

1) I don't want the originals modified, do a "my" assignment

2) I do want the orginals modified, I have to use the @_[x]


Question: is there a way to "alias" a readable name to a @_[x]
value?

>
> In other words: argument handling is call-by-reference.
>
>> In other words, if I called the sub with variables
>> as the arguments and if I modify the value(s) of
>> @_[x] will the variables in the calling argument
>> also get modified?
>
> What happened when you tried it?

Still in the reading/documenting phase. I have a module
with a sub it it that I am test a software revision routine.

Is 39.0.99 newer that 39.0.101, which can be fun! (I did it
in Bash, so I can do it anywhere!)

>
> jue
>

T

unread,
Aug 2, 2015, 6:57:02 PM8/2/15
to
On 08/02/2015 03:48 AM, T wrote:
> Hi All,
>
> Guess who is reading through "perldoc perlsub"?
>


I am noticing on Lines 976 to 989 that the number
and type of variables passed can be defined. For instance:

line 977: sub myvec ($$$) myvec $var, $offset, 1

I have only seen this declaration once where $Bill in this
thread used it.

Is this because Perl "is not so picky" or is there
some other reason why I don't see it?

T

unread,
Aug 2, 2015, 7:04:29 PM8/2/15
to
On 08/02/2015 03:48 AM, T wrote:
> Hi All,
>
> Guess who is reading through "perldoc perlsub"?


I already asked this of Jue a few responses back, but
@_[x] is meaningless to me when trying to maintain a
piece of code.

If I want @_[x] to reflect back to the caller and I
want a human readable variable in the sub, is there
a way I can create an alias (meaning the same variable
under two names) to the @_ argument?



Jürgen Exner

unread,
Aug 2, 2015, 7:57:12 PM8/2/15
to
T <T...@invalid.invalid> wrote:
>Question: is there a way to "alias" a readable name to a @_[x]
>value?
>> In other words: argument handling is call-by-reference.

Same trick as in any other programming language: you can always pass a
reference to an item instead of the item itself.
Or you can create a local reference to the argument with a friendly name
inside of the sub.

And no, I am not going into references. That would be the fourth or
fifth area for you to learn about. Get proficent (or at least get a good
working knowledge) in the others first.

jue

$Bill

unread,
Aug 2, 2015, 8:28:06 PM8/2/15
to
On 8/2/2015 15:38, T wrote:
>
> In "perldoc perlsub" lines 976 to 989, I am not finding
> "\$x". Is this where you are passing the pointer (by
> that I mean address) of "$x" to "mysub"?

perlref pod
...
Making References
References can be created in several ways.

1. By using the backslash operator on a variable, subroutine, or value.
(This works much like the & (address-of) operator in C.) This typically
creates *another* reference to a variable, because there's already a
reference to the variable in the symbol table. But the symbol table
reference might go away, and you'll still have the reference that the
backslash returned. Here are some examples:

$scalarref = \$foo;
$arrayref = \@ARGV;
$hashref = \%ENV;
$coderef = \&handler;
$globref = \*foo;

It isn't possible to create a true reference to an IO handle (filehandle
or dirhandle) using the backslash operator. The most you can get is a
reference to a typeglob, which is actually a complete symbol table
entry. But see the explanation of the *foo{THING} syntax below. However,
you can still use type globs and globrefs as though they were IO
handles.

> Would you define for me what you are doing with "\$x"?
> And was "\" an "escape" or a command?

An operator I believe - it creates a reference to $x.

>> sub mysub { ++${$_[0]}; }
>
> Is "++" the same thing as assembly code's INCREMENT (INC),
> meaning to increment an Integer by 1?

Pre-increment or post-increment (++$x, $x++) - much of Perl is C based.

> And does "{$_[0]}" translate the pointer into the variable?

The braces are sometimes needed and sometimes not depending on if the
syntax is ambiguous or not:

my $y = \$x; # x is 10
++$$y;
print "$x\n"; # prints 11

Complicated stuff referencing/de-referencing - read perlref slowly.

$Bill

unread,
Aug 2, 2015, 8:34:34 PM8/2/15
to
On 8/2/2015 15:56, T wrote:
> On 08/02/2015 03:48 AM, T wrote:
>> Hi All,
>>
>> Guess who is reading through "perldoc perlsub"?
>>
>
>
> I am noticing on Lines 976 to 989 that the number
> and type of variables passed can be defined. For instance:
>
> line 977: sub myvec ($$$) myvec $var, $offset, 1
>
> I have only seen this declaration once where $Bill in this
> thread used it.

Don't think so. I never posted anything on function/sub prototypes.
Mine was in regards to de-referencing.

> Is this because Perl "is not so picky" or is there
> some other reason why I don't see it?

The ($$$) is a function prototype indicating 3 scalars are wanted as args.

T

unread,
Aug 3, 2015, 3:43:31 AM8/3/15
to
On 08/02/2015 05:34 PM, $Bill wrote:
> On 8/2/2015 15:56, T wrote:
>> On 08/02/2015 03:48 AM, T wrote:
>>> Hi All,
>>>
>>> Guess who is reading through "perldoc perlsub"?
>>>
>>
>>
>> I am noticing on Lines 976 to 989 that the number
>> and type of variables passed can be defined. For instance:
>>
>> line 977: sub myvec ($$$) myvec $var, $offset, 1
>>
>> I have only seen this declaration once where $Bill in this
>> thread used it.
>
> Don't think so. I never posted anything on function/sub prototypes.
> Mine was in regards to de-referencing.

Hi $Bill,

On 08/02/2015 08:04 AM, $Bill wrote:
mysub (\$x);
sub mysub { ++${$_[0]}; }

Got the two lines mixed up. I was thinking you said
sub mysub (\$x);
Which you did not. You think you see what you see.


>
>> Is this because Perl "is not so picky" or is there
>> some other reason why I don't see it?
>
> The ($$$) is a function prototype indicating 3 scalars are wanted as args.

Seems like a good practice for readability to me.
I was wondering why I had not seen anyone use it.

-T

T

unread,
Aug 3, 2015, 3:46:18 AM8/3/15
to
I will. Thank you for helping me with this!

-T

T

unread,
Aug 3, 2015, 4:04:02 AM8/3/15
to
Hi All,

Well, using what Bill (Thank You!) just taught me:

<code>
#!/usr/bin/perl
use strict;
use warnings;

my $A = 10;
my $B = \$A; # $B is now a pointer to $A

print "A = $A\n";
++${$B};

print "A = $A\n";
</code>

A = 10
A = 11


So I could do this inside a sub if I wanted the value
of @_[1] reflected back and I also wanted a readable
variable:

my $DayOfWeekPointer = \$_[1];
print "The Day of the week is ${$DayOfWeekPointer}\n";

I am sure Perls has about 3 bazillion other ways to do aliases.
Do you guys have a favorite way?

-T

Love Pointers!




T

unread,
Aug 3, 2015, 4:05:25 AM8/3/15
to
Oops \@_[1] :-[

shar...@hotmail.com

unread,
Aug 3, 2015, 4:53:01 AM8/3/15
to
Yup. the "for" loop.

my $v = 22;
print $v>; # 22
for($var){$_++};
print $v; # 23

Rainer Weikusat

unread,
Aug 3, 2015, 5:28:52 AM8/3/15
to
$Bill <ne...@todbe.com> writes:

[...]

>> And does "{$_[0]}" translate the pointer into the variable?
>
> The braces are sometimes needed and sometimes not depending on if the
> syntax is ambiguous or not:

Syntactically, Perl references are a bit odd because they're not really
like pointer variables in other languages and Perl doesn't have
dereferencing operators. A simple, scalar variable holding a reference
can be used in place of an identifier in an expression referring to some
object, eg, assuming

my $string = 'Hell on wheels';
print($string, "\n");

the following could would be equivalent:

my $indirect_string = \'Hell on wheels';
print($$indirect_string, "\n");

Other example:

my $ra = [1, 2, 3];
print($$ra[0] + $$ra[2], "\n");

But this doesn't work for

my @ra = ([5, 1], 0);
print($$ra[0][0] + $$ra[0][1], "\n");

because $ra[0] is not a 'simple, scalar variable'. It's an expression
returning a reference. An alternate syntax has to be used here, namely,
a block returning a reference can be used in place of a scalar variable
holding one. Considering this,

print(${$ra[0]}[0] + ${$ra[0]}[1], "\n");

works as intended. For the given example, this means that in

sub inc { ++${$_[0]} }

the braces are necessary because $_[0] is an expression and not a scalar
variable. There's nothing 'ambiguous' involved here as the expression

++$$_[0]

has the well-defined meaning of 'increment the first element of the
array $_ refers to'.

Eric Pozharski

unread,
Aug 3, 2015, 5:33:35 AM8/3/15
to
with <mpm6ue$qqg$1...@dont-email.me> T wrote:

*SKIP*
> Is 39.0.99 newer that 39.0.101, which can be fun! (I did it
> in Bash, so I can do it anywhere!)

perldoc version

% perl -Mversion -wle '
version->parse( v1.2.99 ) < version->parse( v1.2.100 ) and
print "Horay!" '
Horay!

Comes with its own hairs though. And yes, ancient perl might be a
showstopper here. Please consider perlbrew (that's a distribution, in
CPAN sense).

--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom

Rainer Weikusat

unread,
Aug 3, 2015, 5:42:25 AM8/3/15
to
T <T...@invalid.invalid> writes:
> On 08/02/2015 03:48 AM, T wrote:
>> Hi All,
>>
>> Guess who is reading through "perldoc perlsub"?
>
>
> I already asked this of Jue a few responses back, but
> @_[x] is meaningless to me when trying to maintain a
> piece of code.

It means 'the xth argument of the current subroutine' and this may well
be enough to interpret it, eg,

sub reverse_sub
{
$_[1] - $_[0]
}

> If I want @_[x] to reflect back to the caller and I
> want a human readable variable in the sub, is there
> a way I can create an alias (meaning the same variable
> under two names) to the @_ argument?

Assigning a reference to a glob cause the thing the references refers to
to become accessible using the corresponding name in the current symbol
table. Visibility of this can be restricted to the lifetime of a lexical
scope via local, eg,

sub reverse_sub
{
local (*v0, *v1) = \@_[0,1];
$v1 - $v0;
}

OTOH, it's more usual to write subroutines such that they don't change
there arguments but return one or more values instead, if only because
this means the arguments are not required to be mutable. When using the
definitions above, reverse_sub(3, 4) would return one but it would be a
(fatal) runtime error when the subroutine body was

$_[1] -= $_[0]

instead.

Rainer Weikusat

unread,
Aug 3, 2015, 5:45:13 AM8/3/15
to
T <T...@invalid.invalid> writes:
> On 08/02/2015 05:34 PM, $Bill wrote:

[...]

>> The ($$$) is a function prototype indicating 3 scalars are wanted as args.
>
> Seems like a good practice for readability to me.
> I was wondering why I had not seen anyone use it.

That's because it doesn't mean what the sentence above claims. ($$$) is
really 'three arguments which will be interpreted in scalar context'.

G.B.

unread,
Aug 3, 2015, 6:56:14 AM8/3/15
to
On 03.08.15 09:00, Eric Pozharski wrote:
> Please consider perlbrew (that's a distribution, in
> CPAN sense).

Also consider not using any Perl version other than
the one installed on the target system by the system
administrators. ;-)

Justin C

unread,
Aug 3, 2015, 9:12:08 AM8/3/15
to
On 2015-08-03, T <T...@invalid.invalid> wrote:
> On 08/03/2015 01:03 AM, T wrote:

[snip]

>> So I could do this inside a sub if I wanted the value
>> of @_[1] reflected back and I also wanted a readable
>> variable:
>>
>> my $DayOfWeekPointer = \$_[1];
>
> Oops \@_[1] :-[

Oh, *so* close.

You were right the first time.

my $DayOfWeek = \$_[1];

[snip]


>> Love Pointers!

... please stop saying "pointer", *every* Perl document you ever read
will call it a reference (or ref for short), so call it a ref so we're
all speaking the same language.


Justin.

--
Justin C, by the sea.

T

unread,
Aug 3, 2015, 3:52:43 PM8/3/15
to
Yes I know. It was just an example. I wasn't specifically looking
for example with three scalers.

The question is why am I not seeing other use this technique.
Everything I have seen just drops the (). It seems to me if you
declare what you are passing with the (), you alert the
reader/maintainer as to what to expect. It seems like a
really good idea. So, why do I not see this in practical use?

T

unread,
Aug 3, 2015, 4:03:54 PM8/3/15
to
On 08/03/2015 05:21 AM, Justin C wrote:
> On 2015-08-03, T <T...@invalid.invalid> wrote:
>> On 08/03/2015 01:03 AM, T wrote:
>
> [snip]
>
>>> So I could do this inside a sub if I wanted the value
>>> of @_[1] reflected back and I also wanted a readable
>>> variable:
>>>
>>> my $DayOfWeekPointer = \$_[1];
>>
>> Oops \@_[1] :-[
>
> Oh, *so* close.
>
> You were right the first time.
>
> my $DayOfWeek = \$_[1];

Poop. I keep getting $ for the element and @ for the whole
thing mixed up.

>
> [snip]
>
>
>>> Love Pointers!
>
> ... please stop saying "pointer", *every* Perl document you ever read
> will call it a reference (or ref for short), so call it a ref so we're
> all speaking the same language.
>
>
> Justin.
>

Hi Justin,

I will get the Perl-eese down eventually.

And Perl folks do use the word "pointer" on occasion:

Pointers/References and Arbitrary Data Structures"


http://alumnus.caltech.edu/~svhwan/prodScript/perlRefsArbitraryStruc.html

Part of the issue at this point is that I understand general
programming better than the specific Perl terms. That
will change with time.

I guess what I have to pick up is switching from general
programing terms to specific Perl terms. "Associative
arrays" and "hashes" would be another example of
general programming terms and Perl terms. Knowing
several other programming languages is not helpful
in this respect.

-T


T

unread,
Aug 3, 2015, 4:09:40 PM8/3/15
to
Hi Sharma,

I am missing the point. I am looking for a way to do human readable
names for the array elements that are passed to a sub and to
associate the name back to the calling element

I suppose I could just copy it to a "my" variable and then
copy it back at the end. Just is not that elegant to me.

my $Inches = $_[1];
do stuff to inches
$_[1] = $Inches

Also, what does the ">" in "$v>" do? Is the ">" printable?

Thank you for helping me with this.

-T

T

unread,
Aug 3, 2015, 4:12:46 PM8/3/15
to
Interesting! Thank you

T

unread,
Aug 3, 2015, 5:32:50 PM8/3/15
to
On 08/03/2015 12:00 AM, Eric Pozharski wrote:
> with <mpm6ue$qqg$1...@dont-email.me> T wrote:
>
> *SKIP*
>> Is 39.0.99 newer that 39.0.101, which can be fun! (I did it
>> in Bash, so I can do it anywhere!)
>
> perldoc version
>
> % perl -Mversion -wle '
> version->parse( v1.2.99 ) < version->parse( v1.2.100 ) and
> print "Horay!" '
> Horay!
>
> Comes with its own hairs though. And yes, ancient perl might be a
> showstopper here. Please consider perlbrew (that's a distribution, in
> CPAN sense).
>


Hi Eric,

Thank you! It seems in Perl, that not only are there a 101 ways
of doing any one thing, but that it ha usually already been done
before by some one else and made into a module!


Some questions on your code example.

1) looking at your code and the perldoc for version, is
the "->" part of the subroutines name or does the "->"
have some other meaning?

2) If common programming language, does

version->parse( v1.2.99 ) < version->parse( v1.2.100 )
and print "Horay!"

expand to

IF ( version->parse( v1.2.99 ) ) is less that
( version->parse( v1.2.100 ) )
THEN
print "Horay!"
END

3) will "print" always be expected to return a TRUE
so that it can be used in an AND statement?

Thank you for helping me with this

-T

Had some one liner fun!

$ perl -Mversion -wle '
if ( version->parse( v1.2.99 ) < version->parse( v1.2.100 ) )
{ print "Horay!" } else { print "Bummer Dude!" } '
Horay!

$ perl -Mversion -wle '
if ( version->parse( v1.2.99 ) < version->parse( v1.2.03 ) )
{ print "Horay!" } else { print "Bummer Dude!" } '
Bummer Dude!




Jim Gibson

unread,
Aug 3, 2015, 6:28:33 PM8/3/15
to
In article <mpogn7$in5$1...@dont-email.me>, <T...@invalid.invalid> wrote:

> The question is why am I not seeing other use this technique.
> Everything I have seen just drops the (). It seems to me if you
> declare what you are passing with the (), you alert the
> reader/maintainer as to what to expect. It seems like a
> really good idea. So, why do I not see this in practical use?

This technique is called "function prototypes," and it's complicated.
See the section titled "Prototypes" in 'perldoc perlfunc' for some of
the potential pitfalls.

--
Jim Gibson

Jim Gibson

unread,
Aug 3, 2015, 6:30:14 PM8/3/15
to
In article <mpohn1$msd$1...@dont-email.me>, <T...@invalid.invalid> wrote:

> On 08/03/2015 01:52 AM, shar...@hotmail.com wrote:
> > my $v = 22;
> > print $v>; # 22
> > for($var){$_++};
> > print $v; # 23
> >
>
...
>
> Also, what does the ">" in "$v>" do? Is the ">" printable?

That looks like a typo to me.

--
Jim Gibson

T

unread,
Aug 3, 2015, 6:38:05 PM8/3/15
to
That is where I got it. I am not sure as to why no one uses
this. I certainly will. Maybe it is my other programming
experience where the variables passed to the procedure are
part of the procedure's declaration?

It did not seem complicated to me. The doc had specific
examples of the variable type and what abbreviation to
use for them.

Maybe Perl is trying to please both sides?

T

unread,
Aug 3, 2015, 6:57:36 PM8/3/15
to
On 08/03/2015 02:32 PM, T wrote:
> less that

less than

stinkin' typos

shar...@hotmail.com

unread,
Aug 4, 2015, 4:18:08 AM8/4/15
to
On Tuesday, 4 August 2015 04:00:14 UTC+5:30, Jim Gibson wrote:
> > On 08/03/2015 01:52 AM, shar...@hotmail.com wrote:
> > > my $v = 22;
> > > print $v>; # 22
> > > for($var){$_++};
> > > print $v; # 23
> > >
> >
> ...
> >
> > Also, what does the ">" in "$v>" do? Is the ">" printable?
>
> That looks like a typo to me.
>
> --
> Jim Gibson

Oops, yes that's a typo. :-(

It should have been:

my $v = 22;
print $v; # 22
for($v){$_++};
print $v; # 23

I was trying to point out to T that for loop
construct also has aliasing built in into it.

shar...@hotmail.com

unread,
Aug 4, 2015, 5:35:02 AM8/4/15
to
On Tuesday, 4 August 2015 01:39:40 UTC+5:30, T wrote:
[snip]
> >
> > Yup. the "for" loop.
> >
> > my $v = 22;
> > print $v>; # 22
> > for($var){$_++};
> > print $v; # 23
> >
>
> Hi Sharma,
>
> I am missing the point. I am looking for a way to do human readable
> names for the array elements that are passed to a sub and to
> associate the name back to the calling element
>
> I suppose I could just copy it to a "my" variable and then
> copy it back at the end. Just is not that elegant to me.
>
> my $Inches = $_[1];
> do stuff to inches
> $_[1] = $Inches
>
> Also, what does the ">" in "$v>" do? Is the ">" printable?
>
> Thank you for helping me with this.
>
> -T
>

The ">" in "$v>" is a typo.
What I was bringing your attention to was the fact
that in perl, aliasing occurs in a for loop also. The for loop aliases the
variable to the scalar $_

To be specific, say ("in your Modula2-speak") we want a "subroutine"
to trim white-space from a variable passed to it, we can do:
1. sub trim { $_[0] =~ s/^\s+|\s+$//g } # aliasing due to @_
2. sub trim { s/^\s+|\s+$//g for $_[0] } # aliasing due to for loop
Then, later, use as: (in void context return value discarded, $var is upgraded.)
trim($var);

You may want to take a look at the Data::Alias module which is a good fit
for your needs. Taken from this module:

A common usage of aliasing is to make an abbreviation for an expression, to avoid having to repeat that (possibly verbose or ugly) expression over and over:

sub rc4 {
alias my ($i, $j, $S) = @_;
my $a = $S->[($i += 1) &= 255];
my $b = $S->[($j += $S->[$i]) &= 255];
$S->[(($S->[$j] = $a) + ($S->[$i] = $b)) & 255]
}

alias my $fi = $self->{FrobnitzIndex};
$fi = $fi > 0 ? $fi - $adj : $fi + $adj;

Rainer Weikusat

unread,
Aug 4, 2015, 7:10:34 AM8/4/15
to
T <T...@invalid.invalid> writes:
> On 08/03/2015 02:45 AM, Rainer Weikusat wrote:
>> T <T...@invalid.invalid> writes:
>>> On 08/02/2015 05:34 PM, $Bill wrote:
>>
>> [...]
>>
>>>> The ($$$) is a function prototype indicating 3 scalars are wanted as args.
>>>
>>> Seems like a good practice for readability to me.
>>> I was wondering why I had not seen anyone use it.
>>
>> That's because it doesn't mean what the sentence above claims. ($$$) is
>> really 'three arguments which will be interpreted in scalar context'.
>
> The question is why am I not seeing other use this technique.
> Everything I have seen just drops the (). It seems to me if you
> declare what you are passing with the (), you alert the
> reader/maintainer as to what to expect. It seems like a
> really good idea. So, why do I not see this in practical use?

IMHO, it is a really good idea and I actually do use prototypes in this
way when writing code I'm not posting here. But $$$ does not mean 'three
scalar values' but 'three values which will be interpreted in scalar
context.

Example:
-----------
sub add_3
{
$_[0] + $_[1] + $_[2];
}

sub add_3p($$$)
{
$_[0] + $_[1] + $_[2];
}

# this works
print(add_3(1, 2, 3), ' ', add_3p(4, 5, 6), "\n");

# as does this
my @a = (1, 2, 3);

print(add_3(@a), "\n");

# but this would be a compiler error
# print(add_3p(@a), "\n");

# and this prints 9
print(add_3p(@a, @a, @a), "\n");
-----------

ie, without prototype, any 'list of three values' can be passed to the
subroutine. With prototype, an array argument will be interpreted in
scalar context which means that the subroutine gets the array length
and not the values.

Eric Pozharski

unread,
Aug 4, 2015, 1:33:40 PM8/4/15
to
with <mpomiu$9ql$1...@dont-email.me> T wrote:
> On 08/03/2015 12:00 AM, Eric Pozharski wrote:
>> with <mpm6ue$qqg$1...@dont-email.me> T wrote:

*SKIP*
> Thank you! It seems in Perl, that not only are there a 101 ways of
> doing any one thing, but that it ha usually already been done before
> by some one else and made into a module!

Not exactly. Down the road it turns out that if you can imagine sex
there's module for this. Like, looking for a bug in my code for two
month, then finding out that bug isn't in my code. It's not in module.
It's in dependency. And it's not a bug, it's a feature.

> Some questions on your code example.
>
> 1) looking at your code and the perldoc for version, is the "->" part
> of the subroutines name or does the "->" have some other meaning?

Yes, it does.

my $aa = { foo => 'bar' };
print $aa->{foo};

Here '->' dereferences a plain reference (what you keep calling the
p-word, what it's not). And... (see below)

> 2) If common programming language, does
>
> version->parse( v1.2.99 ) < version->parse( v1.2.100 )
> and print "Horay!"
>
> expand to
>
> IF ( version->parse( v1.2.99 ) ) is less that
> ( version->parse( v1.2.100 ) )
> THEN
> print "Horay!"
> END

Not exactly. It's more like

IF is_less_than(
version->parse( v1.2.99 ), version->parse( v1.22.100 ) )
THEN
print "Hooray!\n"
END

Except '<' is derived from '<=>', what is overriden by version module to
act on version objects. What is returned by 'parse' method. What is
usual subroutine, except it's in 'version' namespace and it expects
first parameter to be namespace identifier (it's scalar and it should be
some inheritance (no inheritance is one case of inheritance) otherwise
control-flow couldn't reach that subroutine (or something is seriously
broken)) or it's an object ihnerited from 'version', and then come
parameters. And now it's not the subroutine anymore, but the method.
All that made into existance because 'parse' (what's not the method,
it's a constructor) returns reference (not p-word) blessed into
'version' class (or some inherited one). To get unconfused please
consume

perldoc perlboot

and friends (those are in 'SEE ALSO' section at the bottom).

> 3) will "print" always be expected to return a TRUE so that it can be
> used in an AND statement?

In essence -- yes. If passing bytes away would fail deep inside OS
(after syscall has returned) there's no way for perl to know about it,
but if your buffers don't sync you have a bigger problem. Printing over
closed (or un-opened) filehandle is a fatal run-time error. If OS finds
out that file descriptor is closed from other side then it's a sygnal.
In these latter cases control-flow can't possibly reach out-side
'print'.

And -- no, you must not use that co-insidence "in an AND statement".
You'll be beaten to death for doing that. And there's no "AND
statement". Please, use quotes. If quotes aren't enough you can always
retreat to POD syntax, it's understood by anyone.

Is it a quiz?

*CUT*

T

unread,
Aug 4, 2015, 3:43:18 PM8/4/15
to
In my test modules, I am using it too. It has caught me
at least one and made me correct things, which is a good
thing.

And, thank you for the context translation thing. Again, if
you sent a $$$ a four element array, it would bark
at you, which would save you a bunch of debugging
later on.

Question:

In your example:
sub add_3p($$$)
{
$_[0] + $_[1] + $_[2];
}

Is the missing "return" statement "implied"?


Thank you for the help.

-T

T

unread,
Aug 4, 2015, 4:07:24 PM8/4/15
to
Hi Sharma,

Oh, now it makes more sense.

What does "for($v)" do? I thought what went into
() was the number and type of variable that should
be passed.

on your
for($var){$_++};

My notes from http://perlmaven.com/for-loop-in-perl say:
for (INITIALIZE; TEST; STEP) { BODY; }

Your line is missing the "test" and the "step". Does that
mean that they are "implied"? And step is 1? What is the
implied test?

I am still fuzzy as to when things are implied and
when they are implicit. (I will stick with the
implicit as it makes it more readable, well it does for
me anyway.)

For things to be implied, I would think it would
be written:
for (INITIALIZE; [TEST;] [STEP]) { BODY; }

I looked in Perldoc for "loop, for, -f loop, -f for",
so I struck out seeing how they wrote it there.

Ha, you should be proud of me. I know what the $_ is.
It is Perl shorthand for the "I" in "for I=" :-)

Thank you for the help!
-T


T

unread,
Aug 4, 2015, 4:09:20 PM8/4/15
to
Thank you! I am going to read over this really slowly on
Thursday when I have more office time.

Mart van de Wege

unread,
Aug 4, 2015, 4:42:07 PM8/4/15
to
T <T...@invalid.invalid> writes:

>
> Question:
>
> In your example:
> sub add_3p($$$)
> {
> $_[0] + $_[1] + $_[2];
> }
>
> Is the missing "return" statement "implied"?
>
>
Yes.

Like Common Lisp, Perl returns the value of the last expression of the
sub.

Mart

--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.

Mart van de Wege

unread,
Aug 4, 2015, 4:56:04 PM8/4/15
to
T <T...@invalid.invalid> writes:

> My notes from http://perlmaven.com/for-loop-in-perl say:
> for (INITIALIZE; TEST; STEP) { BODY; }
>
Eewww.

Throw that away. The C-style for loop is almost never used in Perl.

The common syntax is

for $loop_variable (@list_of_things_to_loop_over) { do_stuff }

The loop variable is optional, in which case for will loop over the list
assigning each element to the topic variable $_

If you pass a single element, like the previous poster did, then you get
a loop with only a single iteration.

If you don't want to use the loop variable at all, but just want to use
the construct to iterate n times over the block, then you can do what I
did in another post:

for (1..n) { do_stuff }

George Mpouras

unread,
Aug 4, 2015, 5:39:32 PM8/4/15
to
On 2/8/2015 13:48, T wrote:
> Hi All,
>
> Guess who is reading through "perldoc perlsub"?
>
>
> I am a little bit fuzzy as to what happens to values
> that are passed to the sub. On line 92, they give an
> example:
>
> sub max {
> my $max = shift(@_);
> foreach $foo (@_) {
> $max = $foo if $max < $foo;
> }
> return $max;
> }
> $bestday = max($mon,$tue,$wed,$thu,$fri);
>
>
> I makes total sense. In Modula2 and Bash, I call
> this a "function". It returns "a" value.
>
> Now here is the fuzz. Can you use this thing as
> a procedure (a.k.a a subroutine) where you can
> modify the arguments you send it?
>
> In other words, if I called the sub with variables
> as the arguments and if I modify the value(s) of
> @_[x] will the variables in the calling argument
> also get modified? Or does whatever happens in the
> sub not affect the calling arguments?
>
> Many thanks,
> -T
>


the truth is that eventually will turned to heat

Eric Pozharski

unread,
Aug 5, 2015, 5:33:49 AM8/5/15
to
with <mpr62d$nn2$2...@dont-email.me> T wrote:
> On 08/04/2015 02:34 AM, shar...@hotmail.com wrote:
>> On Tuesday, 4 August 2015 01:39:40 UTC+5:30, T wrote:

*SKIP*
> Thank you! I am going to read over this really slowly on Thursday
> when I have more office time.

Consider reading with debugger instead of glaring at it. For starter
you need five commands: 'v', 'l', 's', 'n', and 'x'. And later you will
need 'b'. Also, Perl debugger has in-line help on commands (obvious
'h').

T

unread,
Aug 6, 2015, 4:44:18 PM8/6/15
to
On 08/04/2015 01:38 PM, Mart van de Wege wrote:
> T <T...@invalid.invalid> writes:
>
>>
>> Question:
>>
>> In your example:
>> sub add_3p($$$)
>> {
>> $_[0] + $_[1] + $_[2];
>> }
>>
>> Is the missing "return" statement "implied"?
>>
>>
> Yes.
>
> Like Common Lisp, Perl returns the value of the last expression of the
> sub.
>
> Mart
>

Hi Mart,

Perl has a lot of implied stuff. Than you!

-T


T

unread,
Aug 6, 2015, 10:48:44 PM8/6/15
to
On 08/04/2015 01:50 PM, Mart van de Wege wrote:
> T <T...@invalid.invalid> writes:
>
>> My notes from http://perlmaven.com/for-loop-in-perl say:
>> for (INITIALIZE; TEST; STEP) { BODY; }
>>
> Eewww.
>
> Throw that away. The C-style for loop is almost never used in Perl.
>
> The common syntax is
>
> for $loop_variable (@list_of_things_to_loop_over) { do_stuff }

Do I presume correctly that you are preloading the @array
with stuff?

T

unread,
Aug 6, 2015, 10:51:00 PM8/6/15
to
On 08/04/2015 01:50 PM, Mart van de Wege wrote:
> T <T...@invalid.invalid> writes:
>
>> My notes from http://perlmaven.com/for-loop-in-perl say:
>> for (INITIALIZE; TEST; STEP) { BODY; }
>>
> Eewww.
>
> Throw that away. The C-style for loop is almost never used in Perl.
>
> The common syntax is
>
> for $loop_variable (@list_of_things_to_loop_over) { do_stuff }
>
> The loop variable is optional, in which case for will loop over the list
> assigning each element to the topic variable $_
>
> If you pass a single element, like the previous poster did, then you get
> a loop with only a single iteration.
>
> If you don't want to use the loop variable at all, but just want to use
> the construct to iterate n times over the block, then you can do what I
> did in another post:
>
> for (1..n) { do_stuff }
>
> Mart
>

Hi Mart,

Thank you. I can see the elegance. And yes, I wrote it down.

-T

T

unread,
Aug 7, 2015, 9:44:37 PM8/7/15
to
Hi Eric,

Thank you!

-T

Test on Monday!

Tim McDaniel

unread,
Aug 14, 2015, 7:04:48 PM8/14/15
to
In article <t9btra1djcn039r85...@4ax.com>,
Jürgen Exner <jurg...@hotmail.com> wrote:
>T <T...@invalid.invalid> wrote:
>>Question: is there a way to "alias" a readable name to a @_[x]
>>value?
>>> In other words: argument handling is call-by-reference.
>
>Same trick as in any other programming language: you can always pass a
>reference to an item instead of the item itself.

Excpet for those languages which don't allow taking of references.
I found DSSSL and XSLT to be mind-warping for many reasons.

--
Tim McDaniel, tm...@panix.com

Tim McDaniel

unread,
Aug 14, 2015, 7:13:47 PM8/14/15
to
In article <87mvy8d...@doppelsaurus.mobileactivedefense.com>,
Rainer Weikusat <rwei...@mobileactivedefense.com> wrote:
>OTOH, it's more usual to write subroutines such that they don't change
>there arguments but return one or more values instead, if only because
>this means the arguments are not required to be mutable.

What he said.

Also, aliasing allows what the sub does to leak out to the caller, so
the coder has to be careful to document what arguments it changes, and
has to be careful not to change arguments otherwise.

Perl programmers need to be aware of when automatic aliasing happens,
but I advocate not using subroutine argument aliasing (unless there's
a good reason in a specific case). I advocate returning the changed
values, or if it's more convenient to change the variables in place, I
advocate having the caller pass a reference to make it explicit that
the variable could be changed.

--
Tim McDaniel, tm...@panix.com
0 new messages