Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Ways of quoting

by Aaronrp (Scribe)
on May 14, 2020 at 23:16 UTC ( [id://11116792]=perlmeditation: print w/replies, xml ) Need Help??

I knew that you could use :: to make sure that a package name wasn't used as a subroutine, but I didn't really understand what it did.

$ perl -MO=Deparse -e '$x = anything_could_go_here::' $x = 'anything_could_go_here'; -e syntax OK

Just like single quotes?

TIMTOWTDI I guess...

So we have

q{} '' => :: qw{} - (as in -bareword)

And of course

no strict; $x = bareword;

not to mention

"" and qq{} `` and qx{}

Somebody will tell me that I missed some, I'm sure.

Replies are listed 'Best First'.
Re: Ways of quoting
by davido (Cardinal) on May 15, 2020 at 21:22 UTC

    I have to admit, I hate the idea of the trailing ::.

    If you're enumerating the ways to quote things in Perl, don't forget HERE docs:

    <<HERE # Double quoted <<"HERE" # Double quoted, explicit. <<'HERE' # Single quoted, explicit. <<\HERE # Single quoted, uncommonly used. <<`HERE` # Same as qx or ``. <<~HERE # Double quoted, indented. <<~"HERE" # Double quoted, indented. <<~'HERE' # Single quoted, indented. <<~\HERE # Single quoted, indented, uncommonly used (I'll b +et you could grep all of CPAN and not find this). <<~`HERE` # Same as qx / ``, indented.

    Which made me think, too bad we don't have:

    <<~/HERE/x # Regexp object, indented, multi-line freeform (/ +x modifier).

    Dave

      too bad we don't have: <<~/HERE/x
      my $re = qr/@{[<<'HERE']}/x; f . . HERE

      Close enough? ;-)

        Good enough for an upvote. I tend to shun the "@{[...]}" trick in real code, but this is a creative enough solution it made me smile.


        Dave

Re: Ways of quoting
by LanX (Saint) on May 15, 2020 at 00:30 UTC
    Hi

    IMHO the only purpose of assigning a package's name to a scalar is to handle it later in a referential or symbolic way.

    For instance $x->can("function") will return a code ref if "function" exists in the package.

    So the benefit of writing $x = anything_could_go_here:: over $x = 'anything_could_go_here' is only catching the error when misspelling the package's name.

    (you already demonstrated this after activating warnings )

    I'd strongly discourage using this as golfing hack to avoid single quotes around normal strings.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Yeah, clearly just using it as a sneaky way of quoting doesn't work.

      Having said this, one thing I've done in the past is something like this:

      use Const::Fast; use A::Really::Long::Class::Name::To::Type; const my $CLASS => 'A::Really::Long::Class::Name::To::Type'; $CLASS->do_something; $CLASS->do_something_else; $CLASS->yet_another_thing; # # and a bunch more of these #

      In that circumstance, seems to me that

      const my $CLASS => A::Really::Long::Class::Name::To::Type::;

      might actually be preferable, in that it gives you that check against the package name.

      All I can say is I've been using perl since the '90s and I still have so much I don't know.

Re: Ways of quoting
by kcott (Archbishop) on May 15, 2020 at 07:39 UTC

    G'day Aaronrp,

    "I knew that you could use :: to make sure that a package name wasn't used as a subroutine, but I didn't really understand what it did."

    Take a look at "perlobj: Invoking Class Methods". I think that has the information you're missing.

    "Somebody will tell me that I missed some, I'm sure."

    You missed some. :-)

    "perlop: Quote and Quote-like Operators" has a list. The remainder of that section, and the following two sections, have details.

    — Ken

Re: Ways of quoting
by Aaronrp (Scribe) on May 15, 2020 at 00:05 UTC
    Serves me right.
    $ perl -MO=Deparse -we '$x = anything_could_go_here::' Bareword "anything_could_go_here::" refers to nonexistent package at - +e line 1. Name "main::x" used only once: possible typo at -e line 1. BEGIN { $^W = 1; } $x = 'anything_could_go_here'; -e syntax OK
    So, not exactly the same as single quotes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://11116792]
Approved by LanX
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found