BOK_NEPA has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Single Quote string with Variable
by hippo (Archbishop) on Feb 15, 2023 at 15:31 UTC

    The way you are going about this (shelling out from perl to run perl) is not only crazy and inefficient but it's also unmaintainable as you are currently finding out. Here's the answer to your question but don't you ever write code like this for any real purpose.

    use strict; use warnings; use Test::More tests => 1; my $want = 'aa9957b8d673c62eb1e836cbfcf09719ca41e6e3bf960bba98e74289b8 +8b66fb'; my $email = 'BOK_NEPA@hotmail.com'; # XXX Really awful horrible code to test starts here my $SENDVAR1 = qq(perl -MDigest::SHA=sha256_hex -E 'say sha256_hex(q/$ +email/)'); my $email_SHA256 = qx($SENDVAR1); # XXX Really awful horrible code to test ends here chomp $email_SHA256; diag "Command is >$SENDVAR1<"; is $email_SHA256, $want;

    See also How to ask better questions using Test::More and sample data and the Basic debugging checklist.


    🦛

      Please stop making such awful suggestions. Why would show show someone how to include code injection bugs?!

        you didn't actually read the text did you?
Re: Single Quote string with Variable
by marto (Cardinal) on Feb 15, 2023 at 15:25 UTC

    You already have this, with answers. Explain what you think is wrong or missing with the existing responses, don't spawn another thread.

Re: Single Quote string with Variable
by LanX (Saint) on Feb 15, 2023 at 15:22 UTC
    Why are you shelling out from Perl to call another Perl-Script, instead of use Digest::SHA ?

    For variable interpolation you need qq which is the equivalent of double-quotes "..."

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

Re: Single Quote string with Variable
by ikegami (Patriarch) on Feb 15, 2023 at 21:36 UTC

    You need to build proper Perl code and a proper shell command.

    use String::ShellQuote qw( shell_quote ); sub perl_quote { qq{"\Q$_[0]\E"} } my $email_lit = perl_quote( $email ); my @cmd = ( "perl", "-MDigest::SHA=sha256_hex", "-e", "print sha256_hex( $email_lit )", $email, ); my $cmd = shell_quote( @cmd ); my $email_SHA256 = qx($cmd); die "Can't spawn child: $!\n" if $? == -1; die "Child killed by signal ".( $? & 0x7F )."\n" ) if $? & 0x7F; die "Child exited with error ".( $? >> 8 )."\n" ) if $? >> 8;

    If we pass the address as an argument, we don't have to build any Perl code.

    use String::ShellQuote qw( shell_quote ); my @cmd = ( "perl", "-MDigest::SHA=sha256_hex", "-e", 'print sha256_hex( $ARGV[0] )', $email, ); my $cmd = shell_quote( @cmd ); my $email_SHA256 = qx($cmd); die "Can't spawn child: $!\n" if $? == -1; die "Child killed by signal ".( $? & 0x7F )."\n" ) if $? & 0x7F; die "Child exited with error ".( $? >> 8 )."\n" ) if $? >> 8;

    Better yet, we can avoid the shell, and avoiding building a shell command.

    use IPC::System::Simple qw( capturex ); my @cmd = ( "perl", "-MDigest::SHA=sha256_hex", "-e", 'print sha256_hex( $ARGV[0] )', $email, ); my $email_SHA256 = capturex( @cmd );

    (capturex already provides error handling.)

    As already pointed out, this is silly, though. Because you simply need:

    use Digest::SHA qw( sha256_hex ); my $email_SHA256 = sha256_hex( $email );