Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Unifying namespaces of @name and $name... (bare lexicals)

by LanX (Archbishop)
on Mar 25, 2016 at 18:23 UTC ( #1158789=note: print w/replies, xml ) Need Help??


in reply to Re: Unifying namespaces of @name and $name... (bare lexicals)
in thread Unifying namespaces of @name and $name to simplify dereferencing?

Hello Oiskuu,

Thanks for meditating! =)

I like the idea of sigil less variables, but I doubt this is feasible in Perl, cause there are too many cases where context matters, so you can't avoid @ and % to denote it...

... like in %settings = (%defaults, %arguments)

Perl without sigils would be a kind of JavaScript (which isn't bad, just very reduced)

I always thought of sigils as a way to spare me of Hungarian notation, in order to understand which type a variable has.

Also sigils help avoiding namespace collisions with builtins, like in $length .

> As it stands, mixing arrays and lists come with many nasty pitfalls.

That's why I was meditating about a very simple change, to implicitly replace any my @arr; with my @arr;my $arr = \@arr; .

This could be implemented easily (e.g. macro mechanism) and the effects are obvious.

Though I was naive with the vice versa part, because my $var could be any datatype, so aliasing my @var to @$var wouldn't be obvious.

And one of the most important use cases is passing data structures around, so my $arr = shift would cause a dangerous ambiguity when forcing an automatic alias.

Though an extended syntax like sub foo { my $arr[] = shift; ... } (or my \@arr = shift ) might solve this and implicitly alias @arr (or $arr respectively).

UPDATE

I'm just realizing that something like my $arr[] = shift has an extra benefit of allowing runtime errors if the wrong type is passed to $arr, while writing my $arr = shift would allow to pass whatever you want. :)

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

PS: As a side note:

I was once meditating of adding new sigils for €arrrefs and £hashrefs but this is not only problematic with character encodings but also with keyboard designs.

Just think of The Damians suggestion of appending _ref to each $name. What I do is to prepend $a_... , $h_... , $c_...' . This could be avoided if we had sigils for those references.

Replies are listed 'Best First'.
Re^3: Unifying namespaces of @name and $name... (bare lexicals)
by oiskuu (Hermit) on Mar 28, 2016 at 09:13 UTC

    Well, I did not mean to suggest we do away with sigils altogether. These are just too convenient, if only for string interpolation.

    To illustrate the possibilities: let's say we have the statement

    my foo = [ 1, 2, 3 ];
    This shadows all other symbols foo in scope. Further, any $ @ % sigils on foo are just shorthand for ${} @{} %{}. Now you can write
    print @foo;
    ... and have it work DWIM. Or use the bare foo as reference. But the problem is, if foo refers to code you'll need explicit () to make the call. Hmm. And then, is $doh->foo() a method call or autoboxing? There are probably many caveats I can't think of. So you may be right that this feature wouldn't integrate well.

    Anyhow, the more compact syntax would see more use. Isn't this all about hubrislaziness after all?

    2017-04-01 Embarrassing. I think laziness but write hubris, some times. Blame perl!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1158789]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2019-12-16 07:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?