Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > embedding Variable

Reply
Thread Tools

embedding Variable

 
 
Mike Solomon
Guest
Posts: n/a
 
      10-11-2003
If I write the following code

my $test = "test";

my $output "my test = $test\n";

$test = "NEW TEST";

print $output;

then I will get : my test = test";

is there any way of writing this so as I change $test , $output also changes

Sorry if this a stupid question


Regards Mike

 
Reply With Quote
 
 
 
 
Cognition Peon
Guest
Posts: n/a
 
      10-11-2003



#!/usr/bin/perl -w

my $test = "test";
my $output = output();
print $output;
$test = "NEW TEST";
$output = output();
print $output;

sub output {

return "my test = $test\n";
}


9:37pm, IP packets from Mike Solomon delivered:

>
>
> If I write the following code
>
> my $test = "test";
>
> my $output "my test = $test\n";
>
> $test = "NEW TEST";
>
> print $output;
>
> then I will get : my test = test";
>
> is there any way of writing this so as I change $test , $output also changes
>
> Sorry if this a stupid question
>
>
> Regards Mike
>
>

 
Reply With Quote
 
 
 
 
Tintin
Guest
Posts: n/a
 
      10-12-2003

"Mike Solomon" <(E-Mail Removed)> wrote in message
news:3f8868ac$(E-Mail Removed)...
> If I write the following code
>
> my $test = "test";
>
> my $output "my test = $test\n";
>
> $test = "NEW TEST";
>
> print $output;
>
> then I will get : my test = test";
>
> is there any way of writing this so as I change $test , $output also

changes
>
> Sorry if this a stupid question


My question would be to ask what you are trying to acheive.

I'm almost certain that once we know the problem, there will be a much
better solution/approach to take.


 
Reply With Quote
 
Marco
Guest
Posts: n/a
 
      10-12-2003
Mike Solomon wrote:
> If I write the following code
>
> my $test = "test";
> my $output = "my test = $test\n";
> $test = "NEW TEST";
> print $output;
>
> then I will get : my test = test";
>
> is there any way of writing this so as I change $test , $output also
> changes


Hi Mike,
$output needs to be something that dynamically binds
to the latest value of $test every time. Try this:

my $test = "test";
my $output = sub { "my test = $test\n" };
$test = "NEW TEST";
print $output->(); # or print &$output;

In line 2, $output is initialized to point to an
anonymous subroutine that will re-evaluate $test
every time it's called. Technically, this is known
as a closure.

Line 4 also differs because I now have to dereference
$output to make it behave like the subroutine it
points to.

Marco
----------------------------------------------------
Please remove digits from e-mail address (tr/0-9//d)

 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      10-12-2003
Mike Solomon <(E-Mail Removed)> wrote:

: If I write the following code
:
: my $test = "test";
:
: my $output "my test = $test\n";
:
: $test = "NEW TEST";
:
: print $output;
:
: then I will get : my test = test";
:
: is there any way of writing this so as I change $test , $output also changes

You could do that with a tied scalar.

#!perl
use warnings;
use strict;

package MyString;
sub TIESCALAR {
my $class = shift;
my($fmt, @srefs) = @_;
bless sub{ sprintf $fmt, map $$_, @srefs }, $class;
}
sub FETCH{ shift->() }

package main;
tie my($output), 'MyString', "my test = %s", \(my $test);
$test = 'test';
print "$output\n";
$test = $test = "NEW TEST";
print "$output\n";

tie $output, 'MyString', "My %s has %s.\n", \(my($pet, $pest));
($pet, $pest) = ('dog', 'fleas');
print $output;
($pet, $pest) = ('program', 'bugs');
print $output;

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-12-2003
Mike Solomon <(E-Mail Removed)> wrote:

> If I write the following code


> my $output "my test = $test\n";


> then I will get



A syntax error.



Do not re-type Perl code
Use copy/paste or your editor's "import" function rather than
attempting to type in your code. If you make a typo you will get
followups about your typos instead of about the question you are
trying to get answered.


Like this one.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Mike Solomon
Guest
Posts: n/a
 
      10-12-2003

"Tad McClellan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mike Solomon <(E-Mail Removed)> wrote:
>
> > If I write the following code

>
> > my $output "my test = $test\n";


As Tad pointed out should be my $output = "my test = $test\n";

>
> > then I will get

>
>
> A syntax error.
>
>
>
> Do not re-type Perl code
> Use copy/paste or your editor's "import" function rather than
> attempting to type in your code. If you make a typo you will get
> followups about your typos instead of about the question you are
> trying to get answered.
>
>
> Like this one.
>
>
> --
> Tad McClellan SGML consulting
> (E-Mail Removed) Perl programming
> Fort Worth, Texas


Point taken Tad

Thanks for the help everyone.

I think I will go the sub routine route unless any one else has any bright
ideas


 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      10-13-2003
Cognition Peon <(E-Mail Removed)> wrote in upside down:
> 9:37pm, IP packets from Mike Solomon delivered:
> > my $test = "test";
> >
> > my $output "my test = $test\n";
> >
> > $test = "NEW TEST";
> >
> > print $output;
> >
> > then I will get : my test = test";
> >
> > is there any way of writing this so as I change $test , $output also changes


> #!/usr/bin/perl -w
>
> my $test = "test";
> my $output = output();
> print $output;
> $test = "NEW TEST";
> $output = output();
> print $output;
>
> sub output {
>
> return "my test = $test\n";
> }


Usually one would simply use an _anon_ sub.

my $test = "test";
my $output = sub { "my test = $test\n" };
print $output->();
$test = "NEW TEST";
print $output->();

But if you want $output to look like an ordinary scalar you could use
or a tied scalar or an object that overloads "" depending on when you
want the subtitution to occur...

use Tie::OneOff;
my $test = "test";
tie my $output, 'Tie::OneOff', sub { "my test = $test\n" };
print $output;
my $copy_output = $output; # substition occurs here
$test = "NEW TEST";
print $output;
print $copy_output; # prints "my test = test"


# For reasons I don't understand the current release (0.1) of
# String::Interpolate has stopped working - so don't use it!
use String::Interpolate;
my $test = "test";
my $output = String::Interpolate->new( { test => \$test}, 'my test =
$test\n' );
print $output;
my $copy_output = $output; # substition does not occur here
$test = "NEW TEST";
print $output;
print $copy_output; # prints "my test = NEW TEST"
 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      10-13-2003
(E-Mail Removed) (Jay Tilton) wrote in message news:<(E-Mail Removed)>...
> package MyString;
> sub TIESCALAR {
> my $class = shift;
> my($fmt, @srefs) = @_;
> bless sub{ sprintf $fmt, map $$_, @srefs }, $class;
> }
> sub FETCH{ shift->() }
>
> package main;
> tie my($output), 'MyString', "my test = %s", \(my $test);


I really think you shouldn't have to declare a new package just to
make one-off tied variable...

use Tie::OneOff;
my $test;
tie my $output, 'Tie::OneOff', sub {
sprintf "my test = %s", $test;
};
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Variable 'variable lengths' Taras_96 VHDL 2 05-09-2005 12:58 AM
Convert Character Variable to Integer Variable Brad Smallridge VHDL 2 11-18-2004 01:56 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM
Some questions embedding Pythong in C (callback for variable assignment in the dictionary?) jordi Python 0 07-16-2003 02:31 PM



Advertisments