embedding Variable

Discussion in 'Perl Misc' started by Mike Solomon, Oct 11, 2003.

  1. Mike Solomon

    Mike Solomon Guest

    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
     
    Mike Solomon, Oct 11, 2003
    #1
    1. Advertising

  2. #!/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
    >
    >
     
    Cognition Peon, Oct 11, 2003
    #2
    1. Advertising

  3. Mike Solomon

    Tintin Guest

    "Mike Solomon" <> wrote in message
    news:3f8868ac$...
    > 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.
     
    Tintin, Oct 12, 2003
    #3
  4. Mike Solomon

    Marco Guest

    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)
     
    Marco, Oct 12, 2003
    #4
  5. Mike Solomon

    Jay Tilton Guest

    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

    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;
     
    Jay Tilton, Oct 12, 2003
    #5
  6. Mike Solomon <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 12, 2003
    #6
  7. Mike Solomon

    Mike Solomon Guest

    "Tad McClellan" <> wrote in message
    news:...
    > Mike Solomon <> 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
    > 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
     
    Mike Solomon, Oct 12, 2003
    #7
  8. Mike Solomon

    Guest

    Cognition Peon <> 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"
     
    , Oct 13, 2003
    #8
  9. Mike Solomon

    Guest

    (Jay Tilton) wrote in message news:<>...
    > 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;
    };
     
    , Oct 13, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SW1kYWQ=?=

    Embedding a Control in DataGrid

    =?Utf-8?B?SW1kYWQ=?=, Jan 24, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    607
    Patrick
    Jan 24, 2005
  2. Boris R.
    Replies:
    0
    Views:
    468
    Boris R.
    Oct 15, 2003
  3. jordi
    Replies:
    0
    Views:
    408
    jordi
    Jul 16, 2003
  4. mfglinux
    Replies:
    11
    Views:
    750
    Roberto Bonvallet
    Sep 12, 2007
  5. David Filmer
    Replies:
    19
    Views:
    279
    Kevin Collins
    May 21, 2004
Loading...

Share This Page