in reply to On References to the Unnamed

But  bless \"$_", 'Foo' doesn't even work: Modification of a read-only value attempted...

You could make a "factory" for scalar refs:

sub new_scalar_ref { my $x; \$x }
Of course, this technique can be used in-line with do, if you prefer.

Another idea you could consider is to bless an anonymous sub. That, at least, will throw an exception if a client ever tries to deref it as data.

Replies are listed 'Best First'.
Re^2: On References to the Unnamed
by gaal (Parson) on Mar 15, 2005 at 19:00 UTC
    Golfed:

    sub new_scalar_ref { \(my $x) }

    This is one of The Few Places Where \() Syntax is UsefulTM. Another favorite is DBI->bind_columns().

        Ha! Cool. Hard to beat that. :)

        Inlined that'd be do { \my $x }, I suppose.

        Update: As Anonymous Monk mentions below. This kinda makes me wonder what gensym did except maybe to encapsulate this?

      Another is in Getopt::Long (example slightly golfed)

      GetOptions('in|i=s' => \my $indir, 'out|o=s' => \my $outfile, 'log|l=s' => \my $logfile, 'cp|c=s' => \my $cp, 'help|?' => \my $help, man => \my $man) or pod2usage(2);

      ...it is better to be approximately right than precisely wrong. - Warren Buffet

Re^2: On References to the Unnamed
by tlm (Prior) on Mar 16, 2005 at 00:02 UTC

    What??? It works perfectly well on my system (Linux), both with Perl 5.6.1 and Perl 5.8.4. No error messages at all.

    Perl is an endless source of wonderment...

    the lowliest monk