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

How can I use the string variable expansion for OO "$self->attribute"

0 views
Skip to first unread message

Ilias Lazaridis

unread,
Jun 9, 2007, 5:58:29 AM6/9/07
to
I have working code like this:

{{{
#!perl
my $value = $self->val($attrib);
return "$label: $value";
}}}

I would like to write something like

{{{
#!perl
return "$self->label: $self->val($attrib)";
}}}

is ther ANY way of achieving this, whilst using ONLY the string?

.

--
http://dev.lazaridis.com/lang/ticket/13

Brian McCauley

unread,
Jun 9, 2007, 7:08:07 AM6/9/07
to
On Jun 9, 10:58 am, Ilias Lazaridis <i...@lazaridis.com> wrote:
> I have working code like this:
>
> {{{
> #!perl
> my $value = $self->val($attrib);
> return "$label: $value";
>
> }}}
>
> I would like to write something like
>
> {{{
> #!perl
> return "$self->label: $self->val($attrib)";
>
> }}}
>
> is ther ANY way of achieving this, whilst using ONLY the string?

Define ONLY.

Even "xxx $foo xxx" is just syntactic sugar for "xxx" . $foo . "xxx".

BTW this is FAQ: "How do I expand function calls in a string?"

See also the Interpolate module and Tie::OneOff. (Tie::OneOff was
originally Tie::Simple but was never realeased under that name. Then
someone else released a substancially identical module under the
originl name!).

Ilias Lazaridis

unread,
Jun 9, 2007, 7:57:25 AM6/9/07
to
On Jun 9, 2:08 pm, Brian McCauley <nobul...@gmail.com> wrote:
> On Jun 9, 10:58 am, Ilias Lazaridis <i...@lazaridis.com> wrote:
>
> > I have working code like this:
>
> > {{{
> > #!perl
> > my $value = $self->val($attrib);
> > return "$label: $value";
>
> > }}}
>
> > I would like to write something like
>
> > {{{
> > #!perl
> > return "$self->label: $self->val($attrib)";
>
> > }}}
>
> > is ther ANY way of achieving this, whilst using ONLY the string?
>
> Define ONLY.
>
> Even "xxx $foo xxx" is just syntactic sugar for "xxx" . $foo . "xxx".

I like to use this 'syntactic sugar' (and some more, that's the reason
for this thread).

> BTW this is FAQ: "How do I expand function calls in a string?"

I've searched for "method" and "attributes", as my main interest is OO
Perl.

I'll search in future for "function", too.

"
In general, this is fraught with quoting and readability problems, but
it is possible. To interpolate a subroutine call (in a list context)
into a string:

print "My sub returned @{[mysub(1,2,3)]} that time.\n"
"
http://www.perlmonks.org/?node=How%20do%20I%20expand%20function%20calls%20in%20a%20string%3F

this solution has problems with readability (as already noted by the
author).

> See also the Interpolate module and Tie::OneOff. (Tie::OneOff was
> originally Tie::Simple but was never realeased under that name. Then
> someone else released a substancially identical module under the
> originl name!).

http://search.cpan.org/~nobull/Tie-OneOff/lib/Tie/OneOff.pm
http://perl.plover.com/Interpolation/manual.html

Thanks for the info, but I'm unable to see how I could process this
string:

return "$self->label: $self->val($attrib)"

is there any stand-alone function available (something like
"stringeval"), which would process the string correctly?

If yes (or if I write my own), is there a way to override the default
string-processing behaviour of perl on a per-package basis?

.

--
http://dev.lazaridis.com/lang/ticket/13


Dr.Ruud

unread,
Jun 9, 2007, 8:52:02 AM6/9/07
to
Ilias Lazaridis schreef:

> return "$self->label: $self->val($attrib)";
> is ther ANY way of achieving this, whilst using ONLY the string?

Just play with it:

perl -e'
my ($self, $attrib) = qw(main 42);
sub val{ "<pkg: $_[0], val:$_[1]>" };
sub label{ "my-label" };

print "$_\n" for
$self->label .": ". $self->val($attrib),
qq(@{[ $self->label ]}: @{[ $self->val($attrib) ]}),
sprintf "%s: %s", $self->label, $self->val($attrib),
;
'
my-label: <pkg: main, val:42>
my-label: <pkg: main, val:42>
my-label: <pkg: main, val:42>

--
Affijn, Ruud

"Gewoon is een tijger."

Ilias Lazaridis

unread,
Jun 12, 2007, 6:03:30 AM6/12/07
to
On Jun 9, 3:52 pm, "Dr.Ruud" <rvtol+n...@isolution.nl> wrote:
> Ilias Lazaridis schreef:
>
> > return "$self->label: $self->val($attrib)";
> > is ther ANY way of achieving this, whilst using ONLY the string?
>
> Just play with it:
[...] examples.

I really meant to process this string.

As asked in my other reply:

Brian McCauley

unread,
Jun 12, 2007, 1:19:07 PM6/12/07
to
On Jun 9, 12:57 pm, Ilias Lazaridis <i...@lazaridis.com> wrote:
> On Jun 9, 2:08 pm, Brian McCauley <nobul...@gmail.com> wrote:
> > BTW this is FAQ: "How do I expand function calls in a string?"
>
> print "My sub returned @{[mysub(1,2,3)]} that time.\n"
>
> this solution has problems with readability (as already noted by the
> author).

Everything has problems with readability if abused.


>
> > See also the Interpolate module and Tie::OneOff. (Tie::OneOff was
> > originally Tie::Simple but was never realeased under that name. Then
> > someone else released a substancially identical module under the
> > originl name!).
>

> http://search.cpan.org/~nobull/Tie-OneOff/lib/Tie/OneOff.pmhttp://perl.plover.com/Interpolation/manual.html


>
> Thanks for the info, but I'm unable to see how I could process this
> string:
>
> return "$self->label: $self->val($attrib)"

I know of no way. The trick of creating hashes that are really
functions allows you to get function calls to interpolate (because
they look like hashes).

> is there any stand-alone function available (something like
> "stringeval"), which would process the string correctly?

Not that I know of .

> If yes (or if I write my own), is there a way to override the default
> string-processing behaviour of perl on a per-package basis?

For string literals yes, not for the interpolation rules. At least not
without resorting to source filters.

anno...@radom.zrz.tu-berlin.de

unread,
Jun 13, 2007, 5:59:26 AM6/13/07
to
Ilias Lazaridis <il...@lazaridis.com> wrote in comp.lang.perl.misc:

> On Jun 9, 3:52 pm, "Dr.Ruud" <rvtol+n...@isolution.nl> wrote:
> > Ilias Lazaridis schreef:
> >
> > > return "$self->label: $self->val($attrib)";
> > > is ther ANY way of achieving this, whilst using ONLY the string?
> >
> > Just play with it:
> [...] examples.
>
> I really meant to process this string.
>
> As asked in my other reply:
>
> is there any stand-alone function available (something like
> "stringeval"), which would process the string correctly?

Define "correctly". There are serious parsing problems involved
in recognizing arbitrary code that is embedded in a string.

Anno

Ilias Lazaridis

unread,
Jun 14, 2007, 11:40:38 AM6/14/07
to
On Jun 12, 8:19 pm, Brian McCauley <nobul...@gmail.com> wrote:
> On Jun 9, 12:57 pm, Ilias Lazaridis <i...@lazaridis.com> wrote:
[...] - (readability)
[...] - (TieOff module)

> > Thanks for the info, but I'm unable to see how I could process this
> > string:
>
> > return "$self->label: $self->val($attrib)"
>
> I know of no way. The trick of creating hashes that are really
> functions allows you to get function calls to interpolate (because
> they look like hashes).
>
> > is there any stand-alone function available (something like
> > "stringeval"), which would process the string correctly?
>
> Not that I know of .

ok

> > If yes (or if I write my own), is there a way to override the default
> > string-processing behaviour of perl on a per-package basis?
>
> For string literals yes, not for the interpolation rules. At least not
> without resorting to source filters.

ok.

So, it seems if I want to have somethin other like

return sprintf "%s: %s", $self->label, $self->val($attrib);

that I have to code myself something like e.g.:

return sip "$self->label: $self->val($attrib)";

.

--
http://dev.lazaridis.com/lang/ticket/13

Ilias Lazaridis

unread,
Jun 14, 2007, 12:30:08 PM6/14/07
to
On Jun 13, 12:59 pm, anno4...@radom.zrz.tu-berlin.de wrote:
> Ilias Lazaridis <i...@lazaridis.com> wrote in comp.lang.perl.misc:

With "correctly" I meant "As expected by a programmer".

parsing problems are ther to be solved.

anyway, I've just tried to verify that there is no existent
solution available.

As said in the other answer:

So, it seems if I want to have somethin other like

return sprintf "%s: %s", $self->label, $self->val($attrib);

that I have to code myself something like e.g.:

return sip "$self->label: $self->val($attrib)";

.

--
http://dev.lazaridis.com/lang/ticket/13

.

--
http://dev.lazaridis.com/lang/ticket/13

Mumia W.

unread,
Jun 14, 2007, 9:27:29 PM6/14/07
to
On 06/09/2007 06:57 AM, Ilias Lazaridis wrote:
> [...]

> http://search.cpan.org/~nobull/Tie-OneOff/lib/Tie/OneOff.pm
> http://perl.plover.com/Interpolation/manual.html
>
> Thanks for the info, but I'm unable to see how I could process this
> string:
>
> return "$self->label: $self->val($attrib)"
>

Tie::OneOff seems to be very similar to Tie::Sub:

use strict;
use warnings;
require Tie::Sub;
require NetAddr::IP;

my $net = NetAddr::IP->new('loopback');
tie my %sub, 'Tie::Sub', sub {
my $method = shift;
$net->$method;
};

print "My address is $sub{addr}\n";

Tie::Sub makes a function call look like a reference to a hash element,
and interpolation is done for hash elements within double-quoted strings.

> is there any stand-alone function available (something like
> "stringeval"), which would process the string correctly?
>

Probably not.

> If yes (or if I write my own), is there a way to override the default
> string-processing behaviour of perl on a per-package basis?
>

You could create a source filter. Install Filter::Simple and read these:

perldoc perlfilter
perldoc Filter::Simple

I think Tie::Sub is much simpler.

Ilias Lazaridis

unread,
Jun 16, 2007, 5:53:02 PM6/16/07
to
On Jun 15, 4:27 am, "Mumia W." <paduille.4061.mumia.w

+nos...@earthlink.net> wrote:
> On 06/09/2007 06:57 AM, Ilias Lazaridis wrote:
>
> > [...]
> >http://search.cpan.org/~nobull/Tie-OneOff/lib/Tie/OneOff.pm
> >http://perl.plover.com/Interpolation/manual.html
>
> > Thanks for the info, but I'm unable to see how I could process this
> > string:
>
> > return "$self->label: $self->val($attrib)"
>
> Tie::OneOff seems to be very similar to Tie::Sub:
>
> use strict;
> use warnings;
> require Tie::Sub;
> require NetAddr::IP;
>
> my $net = NetAddr::IP->new('loopback');
> tie my %sub, 'Tie::Sub', sub {
> my $method = shift;
> $net->$method;
> };
>
> print "My address is $sub{addr}\n";
>
> Tie::Sub makes a function call look like a reference to a hash element,
> and interpolation is done for hash elements within double-quoted strings.

This would be too much overhead.

> > is there any stand-alone function available (something like
> > "stringeval"), which would process the string correctly?
>
> Probably not.
>
> > If yes (or if I write my own), is there a way to override the default
> > string-processing behaviour of perl on a per-package basis?
>
> You could create a source filter. Install Filter::Simple and read these:
>
> perldoc perlfilter
> perldoc Filter::Simple

ok, very nice!

> I think Tie::Sub is much simpler.

Personally I would prefer sprintf (if no other solution available, or
no
time to implement a solution).

.

--
http://dev.lazaridis.com/lang/ticket/13

Dr.Ruud

unread,
Jun 20, 2007, 4:56:58 AM6/20/07
to
Ilias Lazaridis schreef:

> So, it seems if I want to have somethin other like
>
> return sprintf "%s: %s", $self->label, $self->val($attrib);
>
> that I have to code myself something like e.g.:
>
> return sip "$self->label: $self->val($attrib)";

See also `perldoc overload`.

Ilias Lazaridis

unread,
Jun 21, 2007, 3:32:17 PM6/21/07
to

Dr.Ruud :

> Ilias Lazaridis schreef:
>
> > So, it seems if I want to have somethin other like
> >
> > return sprintf "%s: %s", $self->label, $self->val($attrib);
> >
> > that I have to code myself something like e.g.:
> >
> > return sip "$self->label: $self->val($attrib)";
>
> See also `perldoc overload`.

http://www.tu-chemnitz.de/docs/perldoc-html/overload.html

looks very interesting.

btw: can I actually overload the "@" and "%", too - thus my custom
Array/Hash class is used?

.

--
http://dev.lazaridis.com/lang/ticket/13

Uri Guttman

unread,
Jun 21, 2007, 6:15:56 PM6/21/07
to
>>>>> "IL" == Ilias Lazaridis <il...@lazaridis.com> writes:


IL> http://www.tu-chemnitz.de/docs/perldoc-html/overload.html

perldoc.perl.org has all the docs and so does your own perl
installation.

IL> btw: can I actually overload the "@" and "%", too - thus my custom
IL> Array/Hash class is used?

overloading is for operators, tie (perldoc perltie) is for replacing
variables with an OO implementation.

uri

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

Ilias Lazaridis

unread,
Jun 21, 2007, 8:56:25 PM6/21/07
to
On Jun 22, 1:15 am, Uri Guttman <u...@stemsystems.com> wrote:

> >>>>> "IL" == Ilias Lazaridis <i...@lazaridis.com> writes:
>
> IL>http://www.tu-chemnitz.de/docs/perldoc-html/overload.html
>
> perldoc.perl.org has all the docs and so does your own perl
> installation.
>
> IL> btw: can I actually overload the "@" and "%", too - thus my custom
> IL> Array/Hash class is used?
>
> overloading is for operators, tie (perldoc perltie) is for replacing
> variables with an OO implementation.

I've looked at this (giving first relevant search hit for 'perltie'):

http://www.tu-chemnitz.de/docs/perldoc-html/perltie.html#Tying-Arrays-array%2c-tying

but I cannot see how I can overload / tie the default array to
MyArray, e.g. with a statement like:

use array 'MyArray';

my @data = ('peter', 'paul');
@data->isa('MyArray'); #=> returns true

.

--
http://dev.lazaridis.com/lang/wiki/PerlOO

Uri Guttman

unread,
Jun 21, 2007, 9:31:29 PM6/21/07
to
>>>>> "IL" == Ilias Lazaridis <il...@lazaridis.com> writes:

IL> I've looked at this (giving first relevant search hit for 'perltie'):

IL> http://www.tu-chemnitz.de/docs/perldoc-html/perltie.html#Tying-Arrays-array%2c-tying

stop using a search engine for perl docs. go to perldoc.perl.org which
has all of the docs. also YOUR perl has all the docs. please learn to
use perldoc as it will save you time and trouble.

IL> but I cannot see how I can overload / tie the default array to
IL> MyArray, e.g. with a statement like:

what is a default array?

IL> use array 'MyArray';

you use a tie statement. there are many tie modules on cpan and plenty
of docs on how to create a tied array.

IL> my @data = ('peter', 'paul');
IL> @data->isa('MyArray'); #=> returns true

that has nothing to do with tying. in fact that looks like a syntax
error. please read the perldoc perltie (don't search for it). look on
cpan for tied array modules as most will have examples of how to do the
tying.

Ilias Lazaridis

unread,
Jun 21, 2007, 9:42:35 PM6/21/07
to
On Jun 22, 1:15 am, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "IL" == Ilias Lazaridis <i...@lazaridis.com> writes:
>
> IL>http://www.tu-chemnitz.de/docs/perldoc-html/overload.html
>
> perldoc.perl.org has all the docs and so does your own perl
> installation.
>
> IL> btw: can I actually overload the "@" and "%", too - thus my custom
> IL> Array/Hash class is used?
>
> overloading is for operators, tie (perldoc perltie) is for replacing
> variables with an OO implementation.

I cannot findout how to implement this below with perltie:


perl [http://search.cpan.org/~nwclark/perl-5.8.8/lib/overload.pm
overload] provides functionality for operators:

{{{
#!perl
use overload
'+' => \&myadd,
'-' => \&mysub;
# etc
...
}}}

is there a similar module to overload the classes of build in types
(like array), something like:

{{{
#!perl
use typeoverload
'@' => 'My::Array',
'%' => 'My::Hash';
# etc
...
}}}

.

--
http://dev.lazaridis.com/lang/ticket/17

Uri Guttman

unread,
Jun 21, 2007, 9:53:00 PM6/21/07
to
>>>>> "IL" == Ilias Lazaridis <il...@lazaridis.com> writes:

IL> I cannot findout how to implement this below with perltie:

IL> perl [http://search.cpan.org/~nwclark/perl-5.8.8/lib/overload.pm
IL> overload] provides functionality for operators:

as i said before tie is for variables. it is NOT related to
overload. don't try to make them the same thing as they do very
different things.

IL> is there a similar module to overload the classes of build in types
IL> (like array), something like:

IL> {{{
IL> #!perl
IL> use typeoverload
IL> '@' => 'My::Array',
IL> '%' => 'My::Hash';
IL> # etc
IL> ...
IL> }}}

it is called tie. it is documented. there are many examples of this on
cpan. look for them and learn how to do this. you can't do this for all
variables at one time. you tie an individual variable to a class which
implements it.

Mumia W.

unread,
Jun 22, 2007, 12:52:52 AM6/22/07
to
On 06/21/2007 08:42 PM, Ilias Lazaridis wrote:
> [...]

> is there a similar module to overload the classes of build in types
> (like array), something like:
>
> {{{
> #!perl
> use typeoverload
> '@' => 'My::Array',
> '%' => 'My::Hash';
> # etc
> ...
> }}}
>
> ..
>

You don't want to do that, and thankfully Perl won't let you. Do you
seriously want to make every low-level Perl array access attempt go
through My::Array?

If it were me, I'd just use the traditional syntax for interpolating
complex expressions within strings @{[ $self->val($attrib) ]}. You seem
to think that's ugly. To each his own.

There is little chance that the path you are on right now will lead you
to success, and there's almost no chance that you'll find a more
efficient and simple solution than Tie::Sub.

Oh well. Good luck.


Ilias Lazaridis

unread,
Jun 22, 2007, 1:02:19 AM6/22/07
to
On Jun 22, 4:31 am, Uri Guttman <u...@stemsystems.com> wrote:

> >>>>> "IL" == Ilias Lazaridis <i...@lazaridis.com> writes:
>
> IL> I've looked at this (giving first relevant search hit for 'perltie'):
>
> IL>http://www.tu-chemnitz.de/docs/perldoc-html/perltie.html#Tying-Arrays...

>
> stop using a search engine for perl docs. go to perldoc.perl.org which
> has all of the docs. also YOUR perl has all the docs. please learn to
> use perldoc as it will save you time and trouble.

please!

I just provide links, thus future readers can follow the thread
immediately.

> IL> but I cannot see how I can overload / tie the default array to

[...] - (tie etc.)

(duplicate message due to problems with google-groups)

relevant comments here:

http://groups.google.com/group/comp.lang.perl.misc/msg/0a355d7ecbd4747c

.

--
http://dev.lazaridis.com/lang/ticket/17

Uri Guttman

unread,
Jun 22, 2007, 1:04:26 AM6/22/07
to
>>>>> "IL" == Ilias Lazaridis <il...@lazaridis.com> writes:

IL> On Jun 22, 4:31 am, Uri Guttman <u...@stemsystems.com> wrote:
>> >>>>> "IL" == Ilias Lazaridis <i...@lazaridis.com> writes:
>>
IL> I've looked at this (giving first relevant search hit for 'perltie'):
>>
IL> http://www.tu-chemnitz.de/docs/perldoc-html/perltie.html#Tying-Arrays...
>>
>> stop using a search engine for perl docs. go to perldoc.perl.org which
>> has all of the docs. also YOUR perl has all the docs. please learn to
>> use perldoc as it will save you time and trouble.

IL> please!

IL> I just provide links, thus future readers can follow the thread
IL> immediately.

but links to other sites which just have perl docs. tell them to use
THEIR OWN perldocs that come with perl.

IL> but I cannot see how I can overload / tie the default array to

IL> [...] - (tie etc.)

IL> (duplicate message due to problems with google-groups)

IL> relevant comments here:
IL> http://groups.google.com/group/comp.lang.perl.misc/msg/0a355d7ecbd4747c

huh?? you haven't made any relevant comments yet.

Ilias Lazaridis

unread,
Jun 22, 2007, 1:05:33 AM6/22/07
to
On Jun 22, 4:53 am, Uri Guttman <u...@stemsystems.com> wrote:

> >>>>> "IL" == Ilias Lazaridis <i...@lazaridis.com> writes:
>
> IL> I cannot findout how to implement this below with perltie:
>
> IL> perl [http://search.cpan.org/~nwclark/perl-5.8.8/lib/overload.pm
> IL> overload] provides functionality for operators:
>
> as i said before tie is for variables.
[...]

> IL> is there a similar module to overload the classes of build in types
> IL> (like array), something like:
>
> IL> {{{
> IL> #!perl
> IL> use typeoverload
> IL> '@' => 'My::Array',
> IL> '%' => 'My::Hash';
> IL> # etc
> IL> ...
> IL> }}}
>

> you can't do this for all variables at one time.

[...]

ok, I summarize that there is no direct construct available to achieve
the above.

thank you for your reply.

.

--
http://dev.lazaridis.com/lang/ticket/17

Ilias Lazaridis

unread,
Jun 22, 2007, 1:14:16 AM6/22/07
to
On Jun 22, 7:52 am, "Mumia W." <paduille.4061.mumia.w
Message has been deleted

Ilias Lazaridis

unread,
Jun 22, 2007, 1:20:35 AM6/22/07
to
On Jun 22, 8:04 am, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "IL" == Ilias Lazaridis <i...@lazaridis.com> writes:
...

> IL> I just provide links, thus future readers can follow the thread
> IL> immediately.
>
> but links to other sites which just have perl docs. tell them to use
> THEIR OWN perldocs that come with perl.

They can decide themselves where they read the docs.

> IL> but I cannot see how I can overload / tie the default array to
> IL> [...] - (tie etc.)
>
> IL> (duplicate message due to problems with google-groups)
>
> IL> relevant comments here:
> IL>http://groups.google.com/group/comp.lang.perl.misc/msg/0a355d7ecbd4747c
>
> huh?? you haven't made any relevant comments yet.

as said, google has some problems (delays, some message seem to not
appear).

.

--
http://dev.lazaridis.com/lang/ticket/17


0 new messages