in reply to Single Quote string with Variable
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 );
|
|---|